0

I have a file where each line looks like this:

cc ssssssss,n

where the two first 'c's are individual characters, possibly spaces, then a space after that, then the 's's are a string that is 8 or 9 characters long, then there's a comma and then an integer.

I'm really new to c and I'm trying to figure out how to put this into 4 seperate variables per line (each of the first two characters, the string, and the number)

Any suggestions? I've looked at fscanf and strtok but i'm not sure how to make them work for this.

Thank you.

user1056805
  • 2,633
  • 5
  • 21
  • 19

3 Answers3

5

I'm assuming this is a C question, as the question suggests, not C++ as the tags perhaps suggest.

  1. Read the whole line in.

  2. Use strchr to find the comma.

  3. Do whatever you want with the first two characters.

  4. Switch the comma for a zero, marking the end of a string.

  5. Call strcpy from the fourth character on to extract the sssssss part.

  6. Call atoi on one character past where the comma was to extract the integer.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

A string is a sequence of characters that ends at the first '\0'. Keep this in mind. What you have in the file you described isn't a string.

I presume n is an integer that could span multiple decimal places and could be negative. If that's the case, I believe the format string you require is "%2[^ ] %9[^,\n],%d". You'll want to pass fscanf the following expressions:

  1. Your FILE *,
  2. The format string,
  3. An array of 3 chars silently converted to a pointer,
  4. An array of 9 chars silently converted to a pointer,
  5. ... and a pointer to int.

Store the return value of fscanf into an int. If fscanf returns negative, you have a problem such as EOF or some other read error. Otherwise, fscanf tells you how many objects it assigned values into. The "success" value you're looking for in this case is 3. Anything else means incorrectly formed input.

I suggest reading the fscanf manual for more information, and/or for clarification.

autistic
  • 1
  • 3
  • 35
  • 80
0

fscanf function is very powerful and can be used to solve your task:

  1. We need to read two chars - the format is "%c%c".
  2. Then skip a space (just add it to the format string) - "%c%c ".
  3. Then read a string until we hit a comma. Don't forget to specify max string size. So, the format is "%c%c %10[^,]". 10 - max chars to read. [^,] - list of allowed chars. ^, - means all except a comma.
  4. Then skip a comma - "%c%c %10[^,],".
  5. And finally read an integer - "%c%c %10[^,],%d".
  6. The last step is to be sure that all 4 tokens are read - check fscanf return value.

Here is the complete solution:

FILE *f = fopen("input_file", "r");

do
{
    char c1 = 0;
    char c2 = 0;
    char str[11] = {};
    int d = 0;

    if (4 == fscanf(f, "%c%c %10[^,],%d", &c1, &c2, str, &d))
    {
        // successfully got 4 values from the file
    }
}
while(!feof(f));

fclose(f);
Sergey Podobry
  • 7,101
  • 1
  • 41
  • 51