Hi, I am having some trouble reading a file the way I would like. What I'm trying to do is extract only the 1st few lines from a file. Here is my current code:
int main(int argc, char** argv)
{
FILE *cpu_info;
char s[80];
cpu_info = fopen("/proc/cpuinfo", "r");
while(fscanf(cpu_info, "%s", s) != EOF)
fprintf(stdout, "%s", s);
}
As you would expect, this is reading the entire file and outputting it to the screen. However, I only want the first couple lines of the file. I don't know how I could do this with fscanf, and that is really the only way I know how to read from a file in C (not C++). Also, how can I format the output(i.e. putting spaces in between strings)? Thanks in advance.