Like Tree1Likes
  • 1 Post By whiteflags

A Small Problem Using fgets() and functions

This is a discussion on A Small Problem Using fgets() and functions within the C Programming forums, part of the General Programming Boards category; Hey guys. I just completed an assignment that had me using arrays to decipher some code. I completed this assignment ...

  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
    2,699
    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
    21,589
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,699
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    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.
    Salem likes this.

  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, 05:42 AM
  3. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 05: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

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