doesn't stop before EOF

This is a discussion on doesn't stop before EOF within the C Programming forums, part of the General Programming Boards category; Enter a filename, enter a number, and the program is supposed to get to that "line" and print it then ...

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

    doesn't stop before EOF

    Enter a filename, enter a number, and the program is supposed to get to that "line" and print it then exit. My plan to accomplish this was count newlines (i < lines) or until EOF and then print a line. However I can't get the program to stop before EOF, it doesn't seem to be paying attention to the i < line part. I've tried swtching || for && in the while loop assuming somehow that was forcing it to wait for the EOF, no help (since it of course should be an && by my logic). I've tried moving the i < lines into the while as an if break and still exact same results.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    
    int main(void)
    {
    	int i = 0, line, check;
    	FILE *fp;
    	char words[MAX+1], fname[MAX+1];
    	
    	puts("Enter filename");
    	fgets(fname, 100, stdin);
    	
    	if((fp = fopen(fname, "r")) == NULL)
    		{
    		fprintf(stdin, "Can't open %s file.\n", fname);
    		exit(1);
    		}
    	
    	puts("Enter the line you wish to see");
    	scanf("%d", &line);
    	while(i < line && (check = fgetc(fp)) != EOF)
    		if(check == '\n')
    			i++;
    	
    	fgets(words, 100, fp);
    	puts("Line contents:");
    	puts(words);
    	
    	if(fclose(fp) != 0)
    		fprintf(stderr,"Error closing file\n");
    	
    	return 0;
    }
    the file I input has the following text
    Code:
    one two three
    test test test
    four five six
    seven eight nine
    ten eleven twelve
    [Session started at 2007-08-03 12:53:56 -0400.]
    Enter filename
    testrun.txt
    Enter the line you wish to see
    2
    Line contents:
    \277`\220`\375

    Prime has exited with status 0.

    and it always prints the same garbage so obviously nothing new is getting read into words, no matter the number I input.

  2. #2
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    I've also tried changing the if(check == '\n') to
    if(check == '\0')
    if(check == '\r')
    if(check == ' ')
    if(check == 32)
    if(check == 't')

    and still no change

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    The string "word" contains nothing throughout the program, so don't expect a legible result, especially something like containing the current line you're on.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    Quote Originally Posted by citizen View Post
    The string "word" contains nothing throughout the program, so don't expect a legible result, especially something like containing the current line you're on.
    fgets(words, 100, fp) is supposed to fill it, but since it only reaches that after EOF it's not going to put anything.

    I have even changed the code to
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    
    int main(void)
    {
    	int i = 0, line, check;
    	FILE *fp;
    	char words[MAX+1], fname[MAX+1];
    	
    	puts("Enter filename");
    	fgets(fname, 100, stdin);
    	
    	if((fp = fopen(fname, "r")) == NULL)
    		{
    		fprintf(stdin, "Can't open &#37;s file.\n", fname);
    		exit(1);
    		}
    	
    	puts("Enter the line you wish to see");
    	scanf("%d", &line);
    	while(i < line && (check = fgetc(fp)) != EOF)
    		if(check == ' ')
    			i++;
    	
    	if (check == EOF)
    		printf("\nEnd of file reached before given line\n");
    	else
    		{
    		fgets(words, 100, fp);
    		puts("Line contents:");
    		puts(words);
    		}
    	
    	if(fclose(fp) != 0)
    		fprintf(stderr,"Error closing file\n");
    	
    	return 0;
    }
    And it reports the "end of file reached" so it is going all the way to EOF and not terminating when i = line or i > line or i isn't getting incremented.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by zmaker5 View Post
    Code:
            fprintf(stdin, "Can't open %s file.\n", fname);
    slkjgfdnsw;rjkltng

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    fprintf(stdin, "Can't open &#37;s file.\n", fname);
    Why are you printing to stdin?

    Code:
    puts("Enter the line you wish to see");
    	scanf("%d", &line);
    	while(i < line && (check = fgetc(fp)) != EOF)
    		if(check == ' ')
    			i++;
    Since you are asking the user to input what line to display, you know that you want to read at least that many lines into the file so a for loop would probably be better. You can then test for end of file and exit the loop if they entered a number that exceeds the number of lines in the file.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    yeah I change it to stdout and it works just the same (even if I test with fake file names with stdin and stdout.)

    I originally was going to use a for loop and then thought a while loop would be the best way to check for EOF and read characters at the same time. I'll try reconverting to a for loop and see what happens.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    ok changed to

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    
    int main(void)
    {
    	int i = 0, line, check;
    	FILE *fp;
    	char words[MAX+1], fname[MAX+1];
    	
    	puts("Enter filename");
    	fgets(fname, 100, stdin);
    	
    	if((fp = fopen(fname, "r")) == NULL)
    		{
    		fprintf(stdout, "Can't open &#37;s file.\n", fname);
    		exit(1);
    		}
    	
    	puts("Enter the line you wish to see");
    	scanf("%d", &line);
    	
    	for(i=0; i < line; i++)
    		{
    		check = fgetc(fp);
    		if (check != EOF)
    			if (check == 't')
    				i++;			
    		else
    			break;
    		}
    		
    //	while(i < line && (check = fgetc(fp)) != EOF)
    //		if(check == ' ')
    //			i++;
    	
    	if (check == EOF)
    		printf("\nEnd of file reached before given line\n");
    	else
    		{
    		fgets(words, 100, fp);
    		puts("Line contents:");
    		puts(words);
    		}
    	
    	if(fclose(fp) != 0)
    		fprintf(stderr,"Error closing file\n");
    	
    	return 0;
    }
    and still getting

    Code:
    [Session started at 2007-08-03 14:54:15 -0400.]
    Enter filename
    testrun.txt
    Enter the line you wish to see
    1
    
    End of file reached before given line
    
    Prime has exited with status 0.
    though I just double checked the file, and found that when I set it to "w" for fopen it still had the other file by the same name (obviously an empty file) and removed it. Now I can't get the program to see the file, where am I supposed to put it (I'm doing this in Xcode as a project, might have to compile it on Terminal since I know where that looks)

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    I don't think there is any problem is the code , I checked the same cod in the vc++ and it is working fine.
    I am just giving the code snippet of what I tried in hurry:-

    #include<stdio.h>

    void main()
    {
    FILE *fp;
    char lineWanted[201], char_file;
    int i= 1, line = 1;

    if(!(fp = fopen("c:\\r1.txt", "r")))
    printf("Cannot open the file");

    while(i < line && (char_file = fgetc(fp)) != EOF)
    if(char_file == '\n')
    i++;
    if(char_file != EOF) {
    fgets(lineWanted, 200, fp);
    puts(lineWanted);
    }

    }

    and I checked that it is giving correct output. I think your logic is correct.
    Last edited by ratneshp; 08-03-2007 at 01:20 PM. Reason: wanted to make some change

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    alright I've got the program relatively working in terminal, part of the problem was fgets was grabbing the \n at the end of entry (dunno why I keep forgetting about that)

    thanks for your help guys

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    ok new problem, now all it does is print out the first line of any file minus the first character, no matter what is input for lines

    new code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 1000
    
    int main(void)
    {
    	int i = 0, line, check;
    	FILE *fp;
    	char words[MAX+1], fname[MAX+1];
    	
    	puts("Enter filename");
    	gets(fname);
    //	fgets(fname, 1000, stdin);
    	
    	if((fp = fopen(fname, "r")) == NULL)
    		{
    		fprintf(stdout, "Can't open %s file.\n", fname);
    		exit(1);
    		}
    	
    	puts("Enter the line you wish to see");
    	scanf("%d", &line);
    	
    	for(i=0; i < line; i++)
    		{
    		check = fgetc(fp);
    		if (check != EOF)
    			if (check == '\n')
    				i++;			
    		else
    			break;
    		}
    		
    	if (check == EOF)
    		printf("\nEnd of file reached before given line\n");
    	else
    		{
    		fgets(words, 100, fp);
    		puts("Line contents:");
    		puts(words);
    		}
    	
    	if(fclose(fp) != 0)
    		fprintf(stderr,"Error closing file\n");
    	
    	return 0;
    }
    Last edited by zmaker5; 08-03-2007 at 01:23 PM.

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    You second try doesn't make any sense. I think it is completely wrong and it is going nowhere. You first logic was correct.

  13. #13
    Registered User
    Join Date
    Aug 2007
    Posts
    3
    I think you should not call fgetc() in a for loop , it should be called in a while loop till EOF.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    39
    ok it's working now
    though could someone explain to me why changing it from i = 0 to i = 1 fixed it? thanks

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX 100
    
    int main(void)
    {
    	int i = 1, line, check;
    	FILE *fp;
    	char words[MAX+1], fname[MAX+1];
    	
    	puts("Enter filename");
    	gets(fname);
    //	fgets(fname, 100, stdin);
    	
    	if((fp = fopen(fname, "r")) == NULL)
    		{
    		fprintf(stdout, "Can't open &#37;s file.\n", fname);
    		exit(1);
    		}
    	
    	puts("Enter the line you wish to see");
    	scanf("%d", &line);
    	while(i < line && (check = fgetc(fp)) != EOF)
    		if(check == '\n')
    			i++;
    	
    	if (check == EOF)
    		printf("\nEnd of file reached before given line\n");
    	else
    		{
    		fgets(words, 100, fp);
    		puts("Line contents:");
    		puts(words);
    		}
    	
    	if(fclose(fp) != 0)
    		fprintf(stderr,"Error closing file\n");
    	
    	return 0;
    }

  15. #15
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Because you're assuming the file starts at line 1. Therefore, you should begin counting the first line as if it starts at 1.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. EOF Explanation Anybody?
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 01-29-2008, 08:09 PM
  2. EOF or not EOF?
    By CornedBee in forum Linux Programming
    Replies: 2
    Last Post: 09-14-2007, 02:25 PM
  3. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  4. whats the deal with EOF really ???
    By gemini_shooter in forum C Programming
    Replies: 7
    Last Post: 03-06-2005, 03:04 PM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM

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