text file copying incorrectly

This is a discussion on text file copying incorrectly within the C Programming forums, part of the General Programming Boards category; I have a text file that looks like this: Code: hello world hello.worldfull this.12345678.87654321 fullness! And my program will basically ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    40

    text file copying incorrectly

    I have a text file that looks like this:

    Code:
    hello world
    hello.worldfull
    this.12345678.87654321
    fullness!
    And my program will basically just open the file, open an output file (name determined by incrementing the 4th to last letter of the filename), and then copy over string by string using fgets and fprintf. Oddly enough, here's the output file:

    Code:
    hello world
    this.12345678.87654321
    fullness!
    Notice the second line is missing. I looked at the hex code of the file and there's nothing funky about it, every line is terminated with a 0D0A pair, including the line that gets wiped. Thus, I suspect there is something wrong with my code... so here it is:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char **argv) {
     FILE *f, *t;
     char fn[256];
     char *s,*k;
     if (argc < 2) {
      printf("Please pass in the file as the argument.");
        return -1;
     }
     strcpy(fn,argv[1]);
     int p = strlen(fn);
     fn[p-5]++;
     if ((f = fopen(argv[1],"r")) == NULL) {
      printf("Input File cannot be opened.");
        return -2;
     }
     if ((t = fopen(fn,"w")) == NULL) {
      printf("Output File cannot be created.");
        fclose(f);
        return -4;
     }
     while (fgets(s,256,f) != NULL) {
        fprintf(t,"%s",s);
     }
     fclose(t);
     fclose(f);
     return 0;
    }
    Notice, the line near the end that says
    fprintf(t,"%s",s);
    I've also tried replacing it with
    fputs(s,t);
    with the exact same effects (line 2 is missing)

    And yes, I do need to approach it this way, copying over a line at a time, because I will eventually be tokenizing the string and doing stuff with the tokens, but I have that under control.

    Thanks in advance
    -IsmAvatar

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    Where is s pointing to? You just declare it as a char pointer, but never actually point it anywhere. Then you proceed to read in data to it. Strange stuff is bound to happen if not a segfault.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    40
    Ah, I thought I might get trouble from having a string pointer with no address, but the compiler didn't give me any warnings or anything. Guess that explains it. I pointed it to fn, since the variable is no longer used at that point and may be recycled (and is the correct size, too), and that fixed it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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