Writing a simple program to write a text file and append


W

W. eWatson

It's been a very long time since I've written a C program. The one below
writes a few items to a text file. I've inserted an fopen with an "a" to
see if I could somehow append to a file by running the program twice.
That doesn't work, so what will?

#include <stdio.h>

int main()
{
FILE *myfile;

/* create a new file if it does not exist.*/

myfile = fopen("outfile.txt","w");
if (myfile != NULL) fopen("outfile.txt","a"); /* append */
fprintf(myfile,"%s","This is just an example\n"); /* writes message */
fprintf(myfile,"%s %f, %i ", "data output:", 3.14152965*0.5, 12345);
fclose(myfile); /*done!*/
printf("\nFile written.\n");
printf("Press Enter key to exit.\n");
getchar(); /* pause and wait for key */
return (0);
}
 
Ad

Advertisements

K

Kaz Kylheku

/* create a new file if it does not exist.*/

myfile = fopen("outfile.txt","w");

"w" will open the file such a way that it is overwritten.
Truncation of the file can happen instantly.
if (myfile != NULL) fopen("outfile.txt","a"); /* append */

This second open of the file likely finds that there is nothing to append to
any more.

What are you using as a reference manual for this function?
 
W

W. eWatson

"w" will open the file such a way that it is overwritten.
Truncation of the file can happen instantly.


This second open of the file likely finds that there is nothing to append to
any more.

What are you using as a reference manual for this function?
Good question. Comprehensive C. On page131 the author has a table for
"r","w", and "a". Mode and meaning. He doesn't have an example for "a",
but maybe I should just use it in the first open instead, and eliminate
the if all together.

Another question. I'm using MinGW, which contains the cpp compiler.
Would this program compile there w/o any changes, or would I need to
follow c++ rules?

The reason I'm even doing this is that a friend has written a c++
program, and doesn't know how to write a text file. His compiler begins
with the word Network. I want to give him a boost by providing a sample
such as the one below. Been a long time since I used c++ too.

I've never used cpp on MinGW, but tried cpp sample.cpp (the program
below) and it objected that no such file existed, but it does. Probably
I'll have to figure what options are used in the compile line.
 
W

W. eWatson

#include<stdio.h>

int
main(void)
{
int c;
FILE *myfile;

myfile = fopen("outfile.txt","a");
if (myfile != NULL) {
fprintf(myfile,"%s","This is just an example\n");
fprintf(myfile,"%s %f, %i ",
"data output:",
3.14152965*0.5,
12345);
puts("\nFile written.");
fclose(myfile);
myfile = fopen("outfile.txt", "r");
if (myfile != NULL) {
while ((c = fgetc(myfile)) != EOF) {
putchar(c);
}
putchar('\n');
}
fclose(myfile);
}
return 0;
}
Good. You might want to look at my response to Kaz about my concern to
code this in c++.
 
I

Ian Collins

Good question. Comprehensive C. On page131 the author has a table for
"r","w", and "a". Mode and meaning. He doesn't have an example for "a",
but maybe I should just use it in the first open instead, and eliminate
the if all together.

Another question. I'm using MinGW, which contains the cpp compiler.
Would this program compile there w/o any changes, or would I need to
follow c++ rules?

All of the C stdio functions are part of C++.
 
Ad

Advertisements

G

Guest

you need to clarify what you want to achieve
Good. You might want to look at my response to Kaz about my concern to
code this in c++.

what? You ask for a C program you get a C program. If you wanted a C++ program why didn't you ask for a C++ program (in clc++)?

Note:
- your compiler is almost certainly both a C compiler and a C++ compiler
- for noddy programs like these C and C++ are almost identical
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top