#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0

    Reading a string from a .txt file in C


    Hi,

    I have a structure and am reading the elements from a .txt file.

    Then I am placing each element into an array.

    The below is the syntax I have used to read the file:- .

    fscanf(inp,"%s%d%d%d%d", individualCards_t.tCardName,
    &individualCards_t.tPointValue,
    &individualCards_t.tAceFlag,
    &individualCards_t.tCourtFlag,
    &individualCards_t.tUsedFlag);

    All of the integer values read ok, and are placed into the array as I would like it. However, the string does not. Using the debugger, it says that each of the elements has 16 stored in them.
    Within the .txt file I have the following structure
    "AC" 1 1 0 0 0 0 0

    Can anybody see why it will not read my string??

    Please help!
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    What is the declaration of your structure? What does some of the actual data look like?

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0
    Here is the declaration of the struct:

    struct {

    char tCardName[NUM_CARDS];
    int tPointValue;
    int tAceFlag;
    int tCourtFlag;
    int tUsedFlag;
    }individualCards_t;

    The data is written in notepad, and is like this

    "AC" 1 0 0 0 0
    "2C" 2 0 0 0 0 ...etc


    I am reading the data from the file cards.txt using the following alorithm:-

    for(iCardIndex=0; iCardIndex < NUM_CARDS; iCardIndex++)

    {

    fscanf(inp,"%s%d%d%d%d", individualCards_t.tCardName,
    &individualCards_t.tPointValue,
    &individualCards_t.tAceFlag, &individualCards_t.tCourtFlag,
    &individualCards_t.tUsedFlag);

    aCurrentDeck[iCardIndex] = individualCards_t.tCardName ; /*each data is placed into the array with this statement

    aCurrentPoints[iCardIndex]=individualCards_t.tPointValue;

    aCurrentAces[iCardIndex]=individualCards_t.tAceFlag;

    aCurrentCourt[iCardIndex]=individualCards_t.tCourtFlag;

    aCurrentUsed[iCardIndex]=individualCards_t.tUsedFlag;
    }


    fclose(inp);
    }

    Thank you.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    First, please enclose your code in "code" tags, see http://forums.devshed.com/misc.php?action=bbcode&s=.

    You need to tell me what the value for NUM_CARDS is. Does your text file actually have quotes around the initial string values? If so, they will be included in the structure. C strings are NULL terminated, you must be sure to leave at least one extra element in your array.

    Keep in mind that this is VERY unsafe coding practice! This is where buffer overflows happen! It is much better to read the data from file into a fixed length buffer (being sure, of course, not to overflow that buffer, see fgets()), analyze the buffer to be sure nothing unexpected happened, then do a sscanf on the buffer. Sure it is extra code, but when your input file gets corrupted and doesn't cause your computer to crash, you will thank me then.

    There are ways to read input in C++ that are safer, but I am an old C programmer and haven't taken to the C++ IO very well (though I am all over the other elements of OOP and love vectors, maps, etc.).

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0
    Thank you for the replies, and for the advice re: code tags!

    NUM_CARDS is 52.

    The strings in the file do have " " around them.

    This is for a uni project, and as bad as it may sound, i'm not too worried about 'unsafe' coding practice. Just a 40% pass mark!
    So far I have coded 508 lines for the whole program, and the majority of marks come from algorithms concerning the control of gameplay within a blackjack game. The project 'includes all learning outcomes for the unit' and file i/o is a very small part of the unit! But I am really beginning enjoy programming, something I wouldn't have said when I started the course! And would love to learn more advanced C after I have handed this project in!

    Thank you
  10. #6
  11. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244

    Ye got two problems...


    First, you need an array of individualCards_t to read in all the values (I think your variable NUM_CARDS is in the wrong spot, but am not going to do your homework for you), you only have the one (so it is constantly being overwritten). Second, your input data has one too many peices of information. fscanf considers linefeeds as white space, so when you read in the first 5 items into the array, then start to read in the second set, you are initially reading in the final zero on the first line. This is what I used as the input data:

    "AC" 1 0 0 0
    "2C" 2 0 0 0

    This code I wrote that will read it into a structure:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NUM_CARDS 52
    
    struct INDIV_CARDS { 
        char tCardName[NUM_CARDS]; 
        int tPointValue; 
        int tAceFlag; 
        int tCourtFlag; 
        int tUsedFlag; 
    }individualCards_t[2]; 
    
    int main(){
        FILE *fin;
        int i;
    
        if ((fin = fopen("file.txt", "r")) == NULL){
            fprintf(stderr, "Unable to open file\n");
            exit(1);
        }
    
        for (i=0; i<2; i++){
            fscanf(fin,"%s%d%d%d%d", individualCards_t[i].tCardName, 
                                    &individualCards_t[i].tPointValue, 
                                    &individualCards_t[i].tAceFlag,
                                    &individualCards_t[i].tCourtFlag, 
                                    &individualCards_t[i].tUsedFlag); 
        }
    
        for (i=0; i<2; i++){
            printf("%s\t%d\t%d\t%d\t%d\n", individualCards_t[i].tCardName, 
                                    individualCards_t[i].tPointValue, 
                                    individualCards_t[i].tAceFlag,
                                    individualCards_t[i].tCourtFlag, 
                                    individualCards_t[i].tUsedFlag); 
        }
    
        fclose(fin);
    
        return 0;
    }
    I presume you will be able to take this example and run with it.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo