C reading from one file and write to another...problem

This is a discussion on C reading from one file and write to another...problem within the C Programming forums, part of the General Programming Boards category; I'm writing a program for class that reads from a file and then replaces a word with another word and ...

  1. #1
    System-7
    Join Date
    Nov 2005
    Posts
    65

    C reading from one file and write to another...problem

    I'm writing a program for class that reads from a file and then replaces a word with another word and writes that to a file. Right now I'm just doing the basic which is reading from one file and then writing to another....Which I thought would be pretty simple. I am familiar with C++ but with this I am confused on how to exactly do it.

    The problem I'm having is that it will only write the last line and for some reason doesnt read the first lines.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX 512
    
    int main(int argc, char *argv[])
    {
      FILE *file, *file2;
      char line[MAX];
    
      if (argc != 4)
      {
        printf("You must enter: ./replace old-string new-string file-name\n");
        exit(1);
      }
      file = fopen(argv[3], "r");
      file2 = fopen("temp", "w");
      while (fgets(line,sizeof(line),file) != NULL);
      {
        /*Write the line */
        fputs(line, file2);
        printf(line);
    
      }
      fclose (file);
      fclose (file2);
      return 0;
    }
    (i'm using it in linux so...it would have to be like: ./replace string1 string2 FILENAME)

    Thanks for any help you can give...or any direction you can point me in,

    Dan

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    while (fgets(line,sizeof(line),file) != NULL);
    That semi-colon is the source of all your troubles.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    System-7
    Join Date
    Nov 2005
    Posts
    65
    Hahaha...I had been trying to figure that out for close to an hour. THANKS!
    I'll have to pay closer attention now

    Thanks again,
    Dan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. reading file problem
    By samsam1 in forum Windows Programming
    Replies: 4
    Last Post: 01-15-2003, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21