Thread: read from .txt file & put into array?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    7

    read from .txt file & put into array?

    oops! I sent this to c# accidently, am I guilty of multiple posts with my first post?

    read from .txt file & put into array?
    Could someone give me a clue how to read a list of names (one per line )from a text file into an array (2d?) for use in a function.
    I can open and close the file Ok. The file was written to with fprintf and I am trying to read from it with fscanf (using &) but my program keeps crashing. Heres a bit of it below

    //function( ) simply prints the correct answer
    {
    FILE *f;
    char s[30][100]; //crashing badly
    int i;
    f = fopen(datafile,"r"); //../correct_answers.txt
    for(i=0;i!=EOF;i++) //strcpy????
    s[i] = fscanf(f,"\n%s",&s) ;

    switch (theQuestion)
    {
    case 1:

    printf("\n%s",s[0]);//doesn't work

    printf("rrrrr"); //this works but I need
    //the answer from the file in here

    break;
    case 2:

    printf("\n%s",s[1]);
    break;

    The .txt file is as follows:-
    Wilfred Owen
    Jane Austen
    Patrick White



    The program is a CGI program and when invoked from the form
    crashes. It works ok if I use string constants in the case statements but I cant get my answers from the text file into the case statements. I'm new to this, lost , lonely and confused.
    Any clues to point me in the right direction would be much appreciated.
    Thank you.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >f = fopen(datafile,"r");
    Always test that the file is open.

    >for(i=0;i!=EOF;i++)
    A better way would be to use an input function as the loop condition.

    >s[i] = fscanf(f,"\n%s",&s) ;
    fscanf returns the number of characters read as an integer, s[i] is a character array. You also try to pass the address of a char (*)[100] to fscanf where it expects just a char *.

    Try something like this instead:
    Code:
    if ( ( f = fopen ( datafile, "r" ) ) == NULL ) {
      perror ( "File open falure" );
      exit ( EXIT_FAILURE );
    }
    
    for ( i = 0; fgets ( s[i], sizeof s[i], f ) != NULL; i++ ) {
      /*
      ** Work with the line from the file.
      */
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    prelude ,your code works!!

    I stuck an else onto it and put the switch into the body of the loop and it works. Not quite perfect yet since it prints the correct answer three times, four times, five times but I can work on this.
    Thank you for your time *100

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    7
    oops again!
    I took the switch out of the loop and this works perfectly.
    Not entirely sure why it needs the empty curly braces after the for loop but it won't work without them.
    Thanks again.

    ///// ok now
    void cor_ans(int Question)
    {
    FILE *f;
    char s[30][100];
    int i;
    if ( ( f = fopen ( datafile, "r" ) ) == NULL )
    {
    printf ("File open falure" );
    exit ( EXIT_FAILURE );
    }
    else
    for ( i = 0; fgets ( s[i], sizeof s[i], f ) != NULL; i++ ) {

    }


    switch (theQuestion)
    {
    case 1:

    printf("%s",s[0]);

    break;
    case 2:

    printf("%s",s[1]);
    break;
    case 3:
    printf("%s",s[2]);
    break;

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    for ( i = 0; fgets ( s[i], sizeof s[i], f ) != NULL; i++ ) {

    }
    You can change this to:
    Code:
     for ( i = 0; fgets ( s[i], sizeof s[i], f ) != NULL; i++ ) 
        ;
    You don't need nothing in the loop, because all your work is done there, you catch the text and check if you aren't in the end of the file.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Vber
    You can change this to:
    Code:
     for ( i = 0; fgets ( s[i], sizeof s[i], f ) != NULL; i++ ) 
        ;
    Even better:
    Code:
    nof_elems = sizeof s / sizeof s[0];
    for ( i = 0; ( fgets ( s[i], sizeof s[i], f ) != NULL ) && ( i < nof_elems ) ; i++ )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Read text from a file into an array
    By ccwash in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2005, 03:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Read from file to an array
    By brooklyn1126 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 08:32 PM