Thread: A Small Problem Using fgets() and functions

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    5

    A Small Problem Using fgets() and functions

    Hey guys. I just completed an assignment that had me using arrays to decipher some code. I completed this assignment successfully and got the correct answer, but I could not follow the instructor's guidelines fully because of a small error which occurred. The error has to do with reading in from a file.

    I have created a file named cquote.txt which contains one line. The line is "TO C OR NOT TO C - THAT IS THE QUESTION!". My goal is to read this line in from file and store it in an array. To accomplish this, I used the fgets() function as seen in my example below.

    Code:
    #include <stdio.h>
    
    FILE *cquote;
    
    main()
    {
    	char code[41];
    	int i;
    	
    	fopen_s(&cquote, "cquote.txt", "r");
    	while (!feof(cquote))
    		fgets(code, sizeof(code), cquote);
    	fclose(cquote);
    
    	
    	for (i = 0; i < 41; i++)
    	{
    		printf("%c", code[i]);
    	}
    	
    	getchar();
    }
    With this code I get my desired output, namely "TO C OR NOT TO C - THAT IS THE QUESTION".

    However, part of the assignment was to use a function to get the input. So now I modify my code and produce this.

    Code:
    #include <stdio.h>
    
    FILE *cquote;
    
    void getLine(char code[]);
    
    main()
    {
    	char code[41];
    	int i;
    	
    	getLine(code);
    	
    	for (i = 0; i < 41; i++)
    	{
    		printf("%c", code[i]);
    	}
    	
    	getchar();
    }
    
    void getLine(char code[])
    {
    	fopen_s(&cquote, "cquote.txt", "r");
    	while (!feof(cquote))
    		fgets(code, sizeof(code), cquote);
    	fclose(cquote);
    }
    However when I execute this code, I do not get the line from the file returned, instead I get a bunch of nonsensical characters. What did I do wrong here? I'm guessing the problem is related to pointers, but I can't figure out what exactly. Anyway, the assignment was turned in with the code placed in the main body, and not in a function. However I would appreciate any input as to why the function did not work correctly.

    Thank you.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In your function sizeof(code) will not give you the number of bytes in the string but the size of the pointer. You will need to pass the size of the array into the function.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by jimblumberg View Post
    In your function sizeof(code) will not give you the number of bytes in the string but the size of the pointer. You will need to pass the size of the array into the function.

    Jim
    But then why does it work when placed in main(), and not when placed in the getLine() function. If sizeof(code) only returns the size of the pointer, shouldn't the program fail to give the correct results when the code is placed in main()?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shamshir
    If sizeof(code) only returns the size of the pointer, shouldn't the program fail to give the correct results when the code is placed in main()?
    In main, is code an array or a pointer?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    No, in main where you have created the array you have the array. But when you pass an array to a function that array decays into a pointer and no longer can get the size of the array.

    Jim

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    50
    the function is also a void so it should not return anything. modify the following lines
    also pass the array through pointer

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, your professor is teaching you a bad habit, see Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    As to your question, another major part of the problem is that sizeof(code) is not going to be what you think it is in that function. When you pass an array to a function, it decays into a pointer to the first element. sizeof(code) therefore means the size of the pointer char *code.

    Yet another issue is that you're printing out a string character by character:

    Code:
    	for (i = 0; i < 41; i++)
    	{
    		printf("%c", code[i]);
    	}
    If the string is ever less than 41 characters long, then you're going to get some weird output or something because you're printing past the end of a string. It's much smarter to print %s or emulate %s; that is, print characters until you find the '\0' character.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    5
    Quote Originally Posted by Abhas View Post
    the function is also a void so it should not return anything. modify the following lines
    also pass the array through pointer
    I'm trying, have no luck yet though. Thanks everyone for the help, at least I know what's wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault with fgets!!!!
    By dustydude in forum C Programming
    Replies: 4
    Last Post: 06-24-2010, 06:50 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  4. Replies: 12
    Last Post: 10-17-2005, 06:49 AM
  5. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM