Problem with reading files

This is a discussion on Problem with reading files within the C Programming forums, part of the General Programming Boards category; Hi all, I wrote a program in linux which parsed the headers of some data files I have. Now, I've ...

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

    Problem with reading files

    Hi all,

    I wrote a program in linux which parsed the headers of some data files I have. Now, I've tried to port it to windows, and everything seems to work except I can't seem to actually read the data! The data is in ASCII format, very simple text files. Why is this failing? This is my code:

    Code:
    int parseFile (char fileName[100], FILE *out)
     {
     int loc=1;	      //loc is the location of the curser in the string
     char dat[200];
     char str[RESULT_LENGTH];
    FILE *inp;
    inp = fopen(fileName, "r");
    if (inp==NULL)
    {
    	printf("Could not open %s for reading.\n", fileName);
    	return 0;
    }
    
    
    //get 'arg'th line of data and parse it into data.csv
    fgets(dat, sizeof(dat), inp);
    loc=loc+getinp(str,'\'',dat,loc);
    fprintf(out, "%s,", str);			//print filename
    loc=loc+getinp(str,'\'',dat,loc);	//search to date
    loc=loc+getinp(str,'\'',dat,loc);	//print date
    fprintf(out, "%s,", str);
    ...
    it goes on for a while like that, but why does it suddenly not work?

    If I instead just use fgets and then print the resulting string, I get ASCII garbage. . .



    Thanks a bunch,
    Dave

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    Are you checking the return result of fgets() ?

    Does this loop print your file successfully?
    while ( fgets( buff, sizeof buff, inp ) != NULL ) fputs( buff, stdout );

    What does getinp() do?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    hmm, yes that loop does successfully print the file. So, now I have a starting point, I'll keep looking at it. Thanks! I'll repost if I am still stuck

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    10
    Well, I've found that whenever my program executes a line with fprintf, nothing is written.
    The fopen command works, but nothing is written to the file. When I finish the program, and try to open the file, it says "unable to read file". . . (but the file is not empty).

    Why won't fprintf work?
    (btw, the FILE pointer is not null)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-28-2006, 01:06 PM
  2. text files & notepad problem
    By bigtamscot in forum C Programming
    Replies: 2
    Last Post: 05-01-2003, 05:41 PM
  3. Replies: 0
    Last Post: 07-12-2002, 02:40 PM
  4. Problem with cgi script - can't rename files
    By bjdea1 in forum C Programming
    Replies: 2
    Last Post: 12-12-2001, 04:09 PM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 08:27 AM

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