Having problems reading a text file.

This is a discussion on Having problems reading a text file. within the C Programming forums, part of the General Programming Boards category; Hi, I've written a function that scans text in an open file and returns the number of lines, as well ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    3

    Having problems reading a text file.

    Hi, I've written a function that scans text in an open file and returns the number of lines, as well as the largest line width. I want to use this information to create a two dimensional array to store the text. However, my program behaves strangley and I don't know why. When I run the program it just freazes. I put a printf() line in the loop of the function to see if anything was happening, and the iCharCount variable just adds continuosly without reseting. I'm pretty new at C, but as far as I can tell this should work. I think the problem is with reading the EOF, since if I replace iCharCount in the printf() function with the character that was just read(ch), it actualy does print the text on the screen, but then gets scrolled up as printf() prints nothing.

    heres the function code:
    At this point the file is already opened using:
    Code:
    FILE *file ;
    if((file = fopen("test.txt","r")) == NULL)
         return(1) ;
    Code:
    int Scan_File(FILE *file,int *iNumLines, int *iLineWidth)
     {
    	 int iCharCount = 0 ;
    	 unsigned char ch = ' ' ;
    
    	 if(file == NULL)
    		 return 0;
    
    	 *iNumLines  = 0 ;
    	 *iLineWidth = 0 ;
    	 
    	 while((ch = fgetc(file)) != EOF)
    	 {
    		 if(ch == '\n') 
    		 {
    			iLineCount++ ;
    			if(iCharCount > *iLineWidth)
    				*iLineWidth = iCharCount ;
    			iCharCount = 0 ;
    			*iNumLines++ ;
    		 }
    		 else
    			iCharCount++ ;
    
    		 printf("\n%d",iCharCount) ;// I added this to see if something was actualy 
    									// happening.
    	 }
    	 printf("Got this far") ;
    I would appreciate any info on this thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    EOF will not fit in a char, signed or no. There's a FAQ on EOF you might want to read. But in a pinch, just change 'ch' to be an integer.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Or you can use feof(), which I presume is also in the FAQ.

    Remerber to close your file after you're done with it. I usually forget.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    207
    Actually, the FAQ recommends against using feof() to control a loop:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    mw
    Blucast Corporation

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah. It's under the section of why not to use feof to control loops.

    [edit] Curses, foiled again! (Beaten by seconds!) [/edit]


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I can see that Dave's coloring abilities really got to quzah

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Eh? Where have you been for the last thousand or so posts I've made?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Perhaps that's what slows him down.

    [edit]And me . . . I'm slow by default.[/edit]
    Last edited by dwks; 08-28-2005 at 04:13 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I've been reading, I've been reading...

  10. #10
    Registered User
    Join Date
    Aug 2005
    Posts
    3
    Thanks for the tip every thing works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading output into a text file
    By pete212 in forum C Programming
    Replies: 8
    Last Post: 04-23-2008, 05:11 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 01:59 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  5. Problems displaying content of a text file
    By melissa in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2001, 05:13 PM

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