Thread: File I/O question

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    19

    File I/O question

    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.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Make a counter. fgets() will grab a line, increment the counter, output the line.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File i/o and ASCII question
    By muzihc in forum C Programming
    Replies: 13
    Last Post: 11-04-2008, 11:46 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. File I/O Question
    By Achy in forum C Programming
    Replies: 2
    Last Post: 11-18-2005, 12:09 AM
  5. Another dumb question about file i/o
    By Cobras2 in forum C++ Programming
    Replies: 23
    Last Post: 03-14-2002, 04:15 PM