• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

C file reading

mpegripper

[H]ard|Gawd
Joined
Oct 8, 2001
Messages
1,212
i have a text file that's tab spaced like so

1 8080 19.2.46.53
2 2637 19.4.55.61


how do i have my C program read this file make sure that there are no (#) in front of it and grab the data, i also want to only get the line of data for the input given when the program is started

i start the program like this
./client 2
that means i want to use the info from line 2 this time


more specifically, i'm asking how to use the strcmp function and how to parse the file

TIA
 
i dont' know exactly how to do it, but you basically want to use a string tokenizer. and i think strcmp is used kinda like:

strcmp(&stringOne, &stringTwo, length) and then if that == 0, they are the same.

i could be wrong, so someone correct me please.
 
Originally posted by mpegripper
how do i use fget to get the correct line of data from the text file?

You'll have to keep reading in line by line until you get to the line you want.
 
Unless you know, absolutely, that each line has a fixed length (in which case you can use the seek functions to find it), you have to just read the preceeding lines.

If you're certain every line is good, you could try using scanf("%d\t%d\t%d.%d.%d.%d".... or something like that, but that's not very robust.

The more robust you want it to be, the harder it gets.
 
how do i use fget to get the correct line of data from the text file?

If every line is the same length, you could probably use fseek() to get to a particular byte in the file.
 
Don't use fseek, the fields arn't likely to be a fixed length, especially if the file is supposed to be editable with a text editor and given what you've posted so far (tab-spaced, ip addresses, etc).

Hoover the file into memory (hope you know how to use realloc properly, welcome to C), use strtok to find the newlines. Processing the lines is easy, but like ameoba said, somewhat complicated if you want to ensure that you can handle errors.
 
Back
Top