简体   繁体   中英

Reading Strings/words and integers from input file

I want to write a little program to read lines from a given .csv/.txt file and print out specific details based on user input.

I'm currently working with a

FILE *input = fopen("Example.csv", "r");

and the input looks like this:

Test000, 40, 0, empty
Test001, 0, -41, empty

Now if I try to to fscanf() from input, it only sets the first char[] and ignores the other variables.

My fscanf() call looks like this:

fscanf(input, "%s , %d , %d , %s", name, &timeA, &timeB, info);

# I'm calling fscanf(...) inside of while()-condition.
# while (fscanf(...) == 4) { *apply logic here* }

So, with this code, fscanf() only ever sets name to 'Test000,', then '40', '0', 'empty' etc., but ignores timeA, timeB, and info.

They are defined as:

char name[51];
int  timeA = 0;
int  timeB = 0;
char info[51];

I really don't know how to circumvent this problem. Any kind of help will be appreciated!

Thank you for your time.

A scanset could be used. %50[^,] will read up to 50 characters or to a comma.

fscanf(input, " %50[^,], %d , %d , %50s", name, &timeA, &timeB, info);

Note the space before &50[^,] to consume leading whitespace.

Check the return of fscanf . In this case 4 will be returned if all four items are successfully scanned.

fscanf() treats consecutive characters until it encounters white-space as part of a single string ( char[] ) - so the best option for you would be to remove the commas in your .txt file, and make your fscanf the following: fscanf(input, "%s %d %d %s", name, &timeA, &timeB, info); - your data should look like: Test000 40 0 empty . That's the most straightforward way of making it work.

If you want it to work with your current data format, fscanf() may not be the best option. You would be better off using some functions form <string.h> .

char data[512];
fgets(data, sizeof (data), input);

strcpy(name, strtok(data), ","));
timea = (int) strtol(strtok(data, ","), NULL, 10);
timea = (int) strtol(strtok(data, ","), NULL, 10);
strcpy(info, strtok(data, ","));

( strcpy and strtok are both avaible in <string.h> , strtol() is available in <stdlib.h> )

strcpy is used to copy "strings".

strtok splits a string (Note that it modifies the string it is passed!).

strtol converts a string to an long (which we cast to int).

There are more secure versions of some of the functions available (ie strtok_r() and strtol() also comes in an int version (so you don't need to cast its return value to int ) called strtod()

If you are on a *nix system, it would be a good idea to run man function_name() (eg man strtok ) to get a better idea of the function prototype and what it does/how it behaves etc. - or you can always read the man pages online, for example the FreeBSD Online Manual Pages where you can search for the function name and read the relevent man page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM