Writing a simple program to write a text file and append

Discussion in 'C Programming' started by W. eWatson, Apr 1, 2012.

  1. W. eWatson

    W. eWatson Guest

    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);
    }
     
    W. eWatson, Apr 1, 2012
    #1
    1. Advertising

  2. W. eWatson

    Kaz Kylheku Guest

    On 2012-04-01, W. eWatson <> wrote:
    > /* 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?
     
    Kaz Kylheku, Apr 1, 2012
    #2
    1. Advertising

  3. W. eWatson

    W. eWatson Guest

    On 4/1/2012 11:34 AM, Kaz Kylheku wrote:
    > On 2012-04-01, W. eWatson<> wrote:
    >> /* 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?

    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. eWatson, Apr 1, 2012
    #3
  4. W. eWatson

    W. eWatson Guest

    On 4/1/2012 11:45 AM, pete wrote:
    > W. eWatson wrote:
    >
    >> 3.14152965

    >
    > ITYM 3.14159265
    >

    Correct.
     
    W. eWatson, Apr 1, 2012
    #4
  5. W. eWatson

    W. eWatson Guest

    On 4/1/2012 11:42 AM, pete wrote:
    > W. eWatson wrote:
    >>
    >> 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);
    >> }

    >
    > #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++.
     
    W. eWatson, Apr 1, 2012
    #5
  6. W. eWatson

    Ian Collins Guest

    On 04/ 2/12 06:49 AM, W. eWatson wrote:
    > On 4/1/2012 11:34 AM, Kaz Kylheku wrote:
    >> On 2012-04-01, W. eWatson<> wrote:
    >>> /* 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?

    > 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++.

    --
    Ian Collins
     
    Ian Collins, Apr 1, 2012
    #6
  7. W. eWatson

    Guest

    On Sunday, April 1, 2012 8:25:12 PM UTC+1, W. eWatson wrote:
    > On 4/1/2012 11:42 AM, pete wrote:
    > > W. eWatson wrote:



    > >> 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.


    you need to clarify what you want to achieve

    > >> 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);
    > >> }

    > >
    > > #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++.


    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
     
    , Apr 2, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sm
    Replies:
    2
    Views:
    585
    Rolf Magnus
    Apr 29, 2005
  2. HALLES
    Replies:
    5
    Views:
    348
    HALLES
    May 31, 2005
  3. HYRY
    Replies:
    10
    Views:
    641
    Bruno Desthuilliers
    Sep 26, 2007
  4. Iulian Ilea
    Replies:
    1
    Views:
    340
    pcx99
    Dec 21, 2006
  5. sahm
    Replies:
    4
    Views:
    717
Loading...

Share This Page