Reading strings from a file

This is a discussion on Reading strings from a file within the C Programming forums, part of the General Programming Boards category; I'm quite new to C and I'm a wee bit stuck here and would be very grateful for any help ...

  1. #1
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Unhappy Reading strings from a file

    I'm quite new to C and I'm a wee bit stuck here and would be very grateful for any help or advice you guys could share.
    Im trying to open a text file and read a string into an array but I don't know if my syntax is wrong or it's just bad programming. Heres the code so far:
    Code:
     /* Include Header Files */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    /* Define Symboli constants */
    
    
    
    /* Type Declerations */
    
    
    
    typedef struct 
    {
    	char name[25];
    	char country[25];
    	int age;
    	float finalScore;
    	float judgesScore[5];	
    }gymnast;
    
    
    /* Function Prototypes */
    
    
    double GetReal(void);
    int GetInteger(void);
    
    
    /* Global Variables */
    
    FILE *fptr;      //Defines a file pointer
    
    /* Main Funtion */
    
    void main(void)
    {
    	int i;
    	char fileName[30];
    	gymnast competitors[15];
    	
    	puts("Please enter the file name with the data");
    	gets(fileName);
            fptr = fopen("fileName", "r");  //Opens the data file that was inputd 
    	if( fptr == 0)
    	{
    		puts("An error occured when opening the file ");
    		exit(1);          //Cheacks the file is valid
    	}
    	
    	for(i = 0; i <=15; i++)
    	{
    	
    	competitors[i].name = fgets(fptr);
    	puts(competitors[i].name);
    	
    	}
    Please help I'm getting really sick of my own silly mistakes and lack of knowledge

    Cheers Gav

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >void main(void)
    main returns an int.

    >fptr = fopen("fileName", "r");
    This tries to open the file called "fileName", not the string contained in the variable fileName. Change it to this:

    fptr = fopen(fileName, "r");

    >fgets(fptr)
    fgets takes three arguments, the array to assign the string to, the size of the array, and the file to read from:

    fgets(competitors[i].name, 25, fptr);

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Thumbs up

    Cheers your the best
    I'm off to try that just now.

    Thanks again
    Gavo

  4. #4
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Talking

    I works perfectly!!
    I can't thank you enough Prelude
    I'm very happy now

    Cheers Gavo

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>for(i = 0; i <=15; i++)
    And another bug.... this loop iterates 16 times, whereas the competitors array has only 15 elements. You will therefore be writing to memory outside of the array when you do this on the last iteration:
    >fgets(competitors[i].name, 25, fptr);

    So, make it
    >>for(i = 0; i <15; i++)

    Remember, you access arrays from 0 to n-1.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12
    Cheers, I saw this bug when I was running my full code.
    Thanks for the help though (I need it )

    Gavo

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    17

    dont get disheartened

    hi man,

    dont ever get disheartened studying c . it's not easy initially
    but

    study slowly without getting disheartened look out for the concepts.

    also do log in to this site and observe other people's problem sot
    that u could

    solve them and thereby learn

    cheers

    sanju

  8. #8
    Registered User Capulet's Avatar
    Join Date
    Dec 2002
    Posts
    12

    Cool

    Thanks, I will try to help anyone I can.
    I've done some java before so it's not too hard but some of the ideas in C are a bit different.
    I'm constantly trying to learn new things and it looks like the people here are really good and don't mind helping us beginners.

    C u Gavo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading strings from a file
    By ldcel in forum C Programming
    Replies: 3
    Last Post: 12-01-2007, 12:31 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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