Posted: . At: 9:35 AM. This was 4 years ago. Post ID: 13932
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



How to append text to a file in C++ and some other tips.


This is a nice example in C. This will append text to a file whenever it is run. This is the proper way to do this in this programming language.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdlib.h>
#include <stdio.h>
 
int main(void) {
	FILE *f;
	char buf[256];
 
	f = fopen("logfile", "a+");
	if(f) {
		fprintf(f, "This is appended to the file.\n");
	}
	fclose(f);
	return 0;
}

This is a more complicated version, this does the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
 
/********************************************************************
* Description:
* Author:  <shoggoth>
* Created at: Wed Jan 19 13:06:21 EST 2011
* Computer: myhost
* System: Linux 2.6.33-ARCH on x86_64
*
* Copyright (c) 2011   All rights reserved.
*
********************************************************************/
 
#include <stdio.h>
#include <time.h>
 
#define format "At this time: %H:%M:%S"
#define text "The user is running this program."
 
int lineofstars (void) {
	int x = 0;
	while (x < 64) {
		printf("*");
		x++;
		if (x == 31) {
			printf("<|>");
		} else if (x == 64) {
			printf("\n-\n");
		}
	}
	return 0;
}
 
int main (int argc, char** argv) {
 
	lineofstars();
 
	char *File;
	char String[60];
	struct tm *ptr;
	time_t tm;
	char length[60];
 
	tm = time(NULL);
	ptr = localtime(&tm);
	strftime(length, 100, format, ptr);
 
	File = "log.txt";
	snprintf(String, 100, "%s, %s\n", length, text);
 
	FILE *f;
	f = fopen (File, "a+");
 
	if (!f) {
		printf("Sorry, I cannot open the file %s.\n", File);
		return 0;
	}
 
	fprintf(f, String);
 
	fflush(stdout);
	fclose(f);
 
	return 0;
}

Remembering to use fflush() and fclose() is important, as well as a proper buffer size. Many older C functions like gets() are susceptible to overflows and it is important to use more modern programming functions properly.

Another function, this is an example of a void function, declared in an include file. This will open a file and print the contents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
struct _kern1 {
	char *File;
	int len;
	char Kyo[40];
} *kern1 = (struct _kern1 *) 0x80;
 
void kernel(const char *File, int len)
{
	FILE *f;
	char Kyo[40];
 
	if (len > 10 or len < 2)
		return;
 
	f = fopen(File, "r");
	if(!f) {
		printf ("Sorry, I cannot open: %s.\n", File);
		printf("Please check your permissions with\n"		\
			"your supervisor. The feature may not\n"	\
			"be compiled and\\or enabled in your\n"		\
			"kernel version. Or a scsi device, eg,\n"	\
			"a USB drive may not be attached.\n");
		return;
	} else {
/* Based on sample code from:
 * www.koders.com/c/fid84CFEFBF311605F963CB04E0F84A2F52A8120F33.aspx
 * Specifically the section on parsing the /proc/version.
 */
		while (feof(f) != 1) {
			fgets(Kyo, len, f);
/*
 * This function is fast, owing to this i feel. especially with gcc 
 * 4.3.2 & glibc 2.5+. it is faster than using: printf (Kyo);
 */
			fprintf (stdout, "%s", Kyo);
			fflush(stdout);
		}
	}
	fclose(f);
}

It is important to print a message if the file cannot be opened, this can tell the user what is going on. Also printing a usage message if no command-line parameters are issued to the program.

1
2
3
4
5
	char* myarg1 = argv[1];
 
	if (!argc or !myarg1) {
		print_menu();
	}

That is a very important part of a console application. And how to check for a certain command-line parameter and then act on it in your program.

1
2
3
4
5
	if (argc > 1 and strncmp(argv[1], "4", BUF) == 0) {
		printf("\t\tSound Card information.\n");
 
		kernel("/proc/asound/cards", 3);
	}

These tips should give you a nice head start into creating your own console application. Very useful code samples.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.