Permalänk
Medlem

[C] Filhantering

Hejsan!

Jag har den senaste tiden gått en C-kurs. Nu har jag kommit till filhantering och där uppstår vissa problem.

Låt säga att jag har en textfil med x-antal rader där varje rad har följande utseende:

Namn|Telenummer|E-mail

Dvs. t.ex.:

Apan Persson|+467034421|apan@persson.com
Birger J. Johansson|65755671|b@jay.com

Det jag behöver hjälp med är hur jag på enklaste sättet ska läsa in de olika delarna.

FILE *file = fopen("asdf.txt", "r"); if(file == NULL ) { printf("\n\tKunde inte \x94ppna filen!\n"); return FALSE; } while(!feof(file)) { /* Någon kod här för att läsa in de tre olika delarna? */ } fclose(file);

Skulle vara tacksam för allmänna idéer för att komma in i rätt tankebanor eller lite kodexempel.

Tack på förhand.

Permalänk
Medlem

fscanf("%s|%s|%s", namn, tele, e-post);

Permalänk
Glömsk

Det finns flera sätt du kan göra detta. Ett exempel är att du läser in 1 tecken i taget till en temporär sträng tills du upptäcker '|' eller '\n'. Börja då om.

Visa signatur

...man is not free unless government is limited. There's a clear cause and effect here that is as neat and predictable as a law of physics: As government expands, liberty contracts.

Permalänk
Medlem

fscanf(file,"%[^|]|%[^|]|%[^|\n]",name,number, email);

Vijjje: Funkar inte.

Visa signatur

Intel Core i7-3770K | NVIDIA Geforce GTX 980 | 16 GB DDR3 | DELL P2415Q | DELL U2711 | DELL U2410

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av MagnusL
fscanf(file,"%[^|]|%[^|]|%[^|\n]",name,number, email);

Vijjje: Funkar inte.

Varför inte? Förutom att jag mena e_post inte e-post. Jag förstår inte riktigt hur din funkar, kan du förklara?

Permalänk
Medlem
Citat:

Ursprungligen inskrivet av Vijjje
Varför inte? Förutom att jag mena e_post inte e-post. Jag förstår inte riktigt hur din funkar, kan du förklara?

Funkar inte att använda %s i detta fall. '|' skippas ju inte av %s alltså skulle %s matcha hela namn|number|email , eller till första mellanslaget. Vilket inte är det man vill i detta fall.

s =
"Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null character ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first."

[ =
Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating null byte. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, [^]0-9-] means the set "everything except close bracket, zero through nine, and hyphen". The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

Visa signatur

Intel Core i7-3770K | NVIDIA Geforce GTX 980 | 16 GB DDR3 | DELL P2415Q | DELL U2711 | DELL U2410

Permalänk
Medlem
Permalänk
Medlem

MagnusL, tack! Precis det jag sökte efter. Jag försökte med något liknande men fick det inte att funka. Så, nu är det bara att koda vidare.