Thread: problem with fprintf

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    10

    problem with fprintf

    Sorry to make this a new thread, but I decided my old post was incredibly poorly worded..

    So, when I try to use fprintf, the program fails to write to the file (the file becomes "unreadable").

    here is the offending code:
    Code:
    out = fopen(inputFile, "w");
    if (out==NULL)
    {
    	printf("Could not open %s for reading.\n", inputFile);
    	mypause();
    	return 0;
    }
    
    
    //get 'arg'th line of data and parse it into data.csv
    loc = 1;
    myfgets(dat, sizeof(dat), inp);
    loc=loc+getinp(str,'\'',dat,loc);
    /*printf(">>>%s<<<", str);
    mypause();
    exit(0);*/
    fprintf(out, "%s,", str);  //print filename
    So, to debug, I added the commented section, and re-ran the program. However, this part works just fine (the string is printed correctly).

    I also tried putting
    Code:
    fprintf(out, "hello world\n");
    mypause();
    exit(0);
    directly after opening the file, which is also successful.

    So why does the program fail as is? This isn't a homework assignment or anything, I just have about 300 of these files to combine into one CSV file, and I thought this method would be faster/more instructive that doing it by hand.

    Thanks!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What, exactly, is wrong with the output file, and what did the input look like?

    [Your error message and file-name-variable are a bit misleading, as the error message says "Can't open file for reading" and the filename is "inputfile"]

    I don't see anything directly wrong in your code, so there's probably something else going on.

    You are not, by any chance opening the SAME file for input and output at the same time? That doesn't work (well, not if you use "w" at least, as that creates an empty file).

    --
    Mats

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    No, I have two files open. . . And yeah, I did name the pointer incorrectly, but that wasn't the problem since it was consistent. I now changed the fopen from "w" to "a" and the program works. . . . So, I guess I am going to open the file for "w", close it, then open it for "a". I still don't understand why that's necessary, but it works so I guess I'll have to leave it at that, unless someone could point me in a better direction?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by glopv2 View Post
    No, I have two files open. . . And yeah, I did name the pointer incorrectly, but that wasn't the problem since it was consistent. I now changed the fopen from "w" to "a" and the program works. . . . So, I guess I am going to open the file for "w", close it, then open it for "a". I still don't understand why that's necessary, but it works so I guess I'll have to leave it at that, unless someone could point me in a better direction?
    If you want to append to a file, then you should be able to open it with "a" all the time, whether it previously existed or not. "w" should work fine - there's probably something else going wrong, that has been "fixed" by changing the code around - you probably need to do some debugging - check the size of your strings to ensure that you're not writing past the end.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 06:00 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 04:19 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM