Thread: Adding data to existing data in an input file?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Adding data to existing data in an input file?

    Hello,

    I am working on a program (other than the attached example).

    At first my FILE was declared in "r" (read) mode, then I realized that in order to write i need it to be in "w" (write) mode, but that ONLY for if I am NOT reading and writing to the same file at the same time.

    So finally, I changed it to "a" (append), which is suppose to allow me to add to the data to the existing data in the input file. But it is not working, can someone please help me?

    Below, is a oversimplified example of what I am trying to do, the input file contains some data such as integers for example 5 and 4. I want to read these integers from the input file and write the sum of 5+4 into the input file that I just read from so the input file that just contained "5 4" will now contain "5 4 9".

    Code:
    #include <stdio.h>
    
    int main (void){
    
    
       int num1, num2, sum; 
       
       
       FILE *ifp;
       
       ifp = fopen("input.txt", "a");
       
       fscanf(ifp, "%d", num1);
       fscanf(ifp, "%d", num2);
       sum=num1+num2;
       
       fprintf(ifp, "%d", sum);
       
       fclose(ifp);
    
    return 0;
    }
    Thanks in advance.
    Last edited by matthayzon89; 11-20-2010 at 08:14 PM.
    Linklists use recursion to store what? ..... floats!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are two ways to write to a file stream - write (some variety of), and append.

    You need append. If you can't get a valid file pointer, you may not have closed the file in the mode you were using previously.

    Why not make up a wee little example of it - should be very easy, and will perhaps set your mind straight about how the whole file access modes, can be used in this instance.

    If not that, then put a step by step of what your programs data flow is. Are you checking your file pointers to see if they're valid?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Frankly, I would suggest that you read from one file, write to a second file, delete the original file then rename the new file... this way you can keep two separate file pointers and should something go wrong, the original file is not damaged.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by Adak View Post
    There are two ways to write to a file stream - write (some variety of), and append.

    You need append. If you can't get a valid file pointer, you may not have closed the file in the mode you were using previously.

    Why not make up a wee little example of it - should be very easy, and will perhaps set your mind straight about how the whole file access modes, can be used in this instance.

    If not that, then put a step by step of what your programs data flow is. Are you checking your file pointers to see if they're valid?
    Yes I did check that the file pointers are valid and they are.

    Can someone please help my small sample code work (its in the original post)? it does not print correctly...
    Linklists use recursion to store what? ..... floats!

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by matthayzon89 View Post
    Hello,

    I am working on a program (other than the attached example).

    At first my FILE was declared in "r" (read) mode, then I realized that in order to write i need it to be in "w" (write) mode, but that ONLY for if I am NOT reading and writing to the same file at the same time.

    So finally, I changed it to "a" (append), which is suppose to allow me to add to the data to the existing data in the input file. But it is not working, can someone please help me?

    Below, is a oversimplified example of what I am trying to do, the input file contains some data such as integers for example 5 and 4. I want to read these integers from the input file and write the sum of 5+4 into the input file that I just read from so the input file that just contained "5 4" will now contain "5 4 9".

    This has some problems, noted inside:
    Code:
    #include <stdio.h>
    
    int main (void){
    
    
       int num1, num2, sum; 
       
       
       FILE *ifp;
       
       ifp = fopen("input.txt", "a");
       
    /* When you're at the end of the file, there's no more data to be read, so these
        lines do nothing */
    
       fscanf(ifp, "%d", num1);  //missing an & anyway
       fscanf(ifp, "%d", num2);  //ditto
    /* sum wasn't set to 0, so it could be *anything* now */
       sum=num1+num2;
       
       fprintf(ifp, "%d", sum);  
       
       fclose(ifp);
    
    return 0;
    }
    Here's an example with an input file with three numbers in it 30, 20 and 50, each on their own line, in the file.

    Code:
    #include <stdio.h>
    
    int main (void){
    
       char str[50];
       int num1, num2, num3, sum; 
       
       
       FILE *ifp;
       
      //so I can read all data in the file
       ifp = fopen("input.txt", "r+w");
       
       fscanf(ifp, "%d", &num1);
       printf("\n%d", num1);
       fscanf(ifp, "%d", &num2);
       printf("\n%d", num2);
       fscanf(ifp, "%d", &num3);
       printf("\n%d", num3);
       sum=0;
       sum=num1+num2+num3;
       //I want to append this, so I fseek() over the \n on the end of the number3 row
       fseek(ifp, 0, SEEK_END);
       fprintf(ifp, "%d", sum);
       
       fclose(ifp);
       ifp = fopen("input.txt", "a"); //just to show it works
       rewind(ifp);  //but I do need the first byte of the file
      //read all data and print it
       while((fgets(str, sizeof(str), ifp)) != NULL) {
         printf("%s", str);
    
       }
    
      fclose(ifp);
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar();
    return 0;
    }
    Sorry for the example code being a bit clumsy, but hope it helped.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. reading a columns input data file
    By vk13wp in forum C Programming
    Replies: 6
    Last Post: 04-28-2003, 01:32 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM