A "string" in C is the address of a character buffer. You want scanf to fill the memory in the buffer, which is pointed to by the variable. In contrast, an int is a block of memory, not an address. In order for scanf to fill that memory, you need to pass its address.
Use the fscanf Function to Read File Line by Line in C The latter can be used to read the regular file line by line and store them in the buffer. fscanf takes formatting specification similar to the printf specifiers, and all of them are listed in full detail on this page.
fscanf type specifiers No null character is appended at the end. String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).
Each call to fscanf() reads one line from the file.
hi i have a file which contains following lines:
wwa weweweof ewewe wdw:
1 11 ms <1 ms <1 ms 174.78.134.1
2 11 ms <1 ms <1 ms 174.78.134.1
3 5 ms <1 ms <1 ms x58trxd00.abcd.edu.com [143.71.290.42]
4 11 ms <1 ms <1 ms 174.78.134.1
i am using
if(linecount == 8)
while( fscanf(fp, "%d %s %s %s %s %s %s",&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8) != EOF ){
printf("%s",ch);
}
else if (linecount == 9){
while( fscanf(fp, "%d %s %s %s %s %s %s %s",&a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9) != EOF ){
printf("%s",ch);
}
}
how do i check if the line rows in the file contain 8 or 9 elements, so that i can run the above else-if statements accordingly?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With