1

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.

5
  • 2
    So.. C or C++? You should really only tag this with the language you are interested in. :) Commented Mar 1, 2013 at 7:32
  • If this is a C question, why is this tagged C++? Commented Mar 1, 2013 at 7:32
  • 1
    Try strtok. Commented Mar 1, 2013 at 7:32
  • 1
    it's a c question, I removed the c++, sorry. Commented Mar 1, 2013 at 7:32
  • I was confused by the examples I found of strtok and so I wasn't sure if it was what I was looking for. If it is then I'll try to understand it so I can use it. thanks Commented Mar 1, 2013 at 7:35

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you explain step 4 please.
char *comma = strchr(string, ','); ... *comma = 0; So now instead of foo,bar, you have foo<end_of_string>bar allowing you to copy out the foo part.
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.

Comments

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);

Comments

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.