4

I'm trying to store different values that are taken from a file line by line. The lines in the text file read as something shown below

100000,player1,long title name
300000,someotherplayer,another long title name
45512845,thisplayer,one more long title name

I want to store each value that is comma separated into three different arrays, (int)number, (str)player_name, (str)title_name.

I have some code below, but it doesn't compile.

ptr_file=fopen("text.txt", "r");
char buffer[1000];
int line;
line = 0;

while(fgets(buffer, sizeof(buffer), ptr_file) != NULL){
    char number[line]=strtok(buffer, ",");  
    char player_name[line]=strtok(NULL, ",");
    char title_name[line]=strtrok(NULL, ",");
}

Can someone give me some advice on this?

3
  • 2
    So what's the compiler error? Commented Nov 23, 2012 at 4:02
  • Have you forgot the * before identifier in your variable declarations? Commented Nov 23, 2012 at 4:07
  • "o" is wrong, but it's runtime error. ptrfile vs. ptr_file is surely compile time error. and contents of the loop is some kind of heresy Commented Nov 23, 2012 at 4:09

3 Answers 3

2

So, there are a couple of issues with your code,

You open the file in mode "o" which I'm not really sure what it is, I suspect you want "r" strtok returns a char * which you cannot assign to a char[]. One the second run through the loop you will overwrite the data in buffer. I would do something like this:

struct player {
    int number;
    char player_name[64];
    char title_name[256];
};

int main(void) {
    FILE *ptrfile=fopen("text.txt", "r");
    char buffer[1000];
    int line;
    struct player players[16];
    line = 0;
    if(ptrfile==NULL) return 0;
    while(fgets(buffer, sizeof(buffer), ptrfile) != NULL){
        if(strcmp(buffer, "") == 0) return 0;
        char *number=strtok(buffer, ",");
        char *player_name=strtok(NULL, ",");
        char *title_name=strtok(NULL, ",");
        players[line].number=atoi(number);
        strcpy(players[line].player_name, player_name);
        strcpy(players[line].title_name, title_name);;
        line++;
    }
    fclose(ptrfile);
    return 0
}
Sign up to request clarification or add additional context in comments.

1 Comment

Officially, you should check the memory allocations, or comment that you're ignoring error checking.
1

function strtok return a pointer, so it should be

char* p = strtok(...)

Check the reference here

Comments

1

This is something I did that was similar to what you seem to be doing. The problem you will find is that you want to make each value into a char* but you have to malloc each one then you can connect this char* into the array. It would also just be easier to do that with the numbers to then turn them into int later on.

#include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main()
    {
        char *msg[100]; 
        char temp[100];
        int length, i;
        int num = 0;


        while((scanf("%s", &temp[0]) != EOF))
        {
            length = strlen(temp);
            msg[num] = malloc((length+1 )* sizeof(char));
            strcpy(msg[num], temp);
            num++;
        }

        printf("There are %d words in the this input.\n", num);

        for(i = 0; i < num; i++)
        {
            printf("%s\n", msg[i]);
        }
        return 0;
    }

The thing with the malloc is that you will have to have each one unique because the words are all different sizes. I know this example isn't exactly what your doing but it will get you in the right direction.

1 Comment

Officially, you should check the memory allocations, or comment that you're ignoring error checking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.