how do I skip 3 header lines

Posted on 2005-04-07
Last Modified: 2010-04-01
Hi experts,

My file has 3 header lines which I want to skip. How can I do that ?

I have the followinfs to skip one header line:

int isFirstLine=0;
if (isFirstLine == 0)
    isFirstLine = 1;

Please advise how to skip 3 header lines. Thanks
Question by:justinY
    LVL 16

    Expert Comment

    It might be simpler to simply read the first three lines at the very start, and discard them; rather than trying to handle it all in the same loop.

    Psuedo code wise:

    open file
    for three iterations
         read line

    // now process file
    while not end of file
       read line
       process it

    LVL 1

    Accepted Solution

    Using fgets:
    FILE * f = fopen ("NameOfFile.txt", "r+t");
    if (!f) {
      printf ("Could not open file?\n");
      exit (1);
    } else {
      char someBigBuffer [4096];
      for (int i = 0; i < 3; i++)
        fgets (someBigBuffer, 4096, f);

    This will work if it's a text file. If you wanna skip 3 "headers" and the "headers" are of constant size (say you have something like this):
    struct FHeader {
      char Name[36];
      int something;
      int somethingElse;

    You could:
    FILE * f = fopen ("NameOfFile", "r+b");
    if (!f) {
      printf ("Could not open file?\n");
      exit (1);
    } else {
      fseek (f, sizeof (FHeader)*3, SEEK_SET);

    LVL 16

    Expert Comment

    Did either of those answers help?

    If so, it is time to select one and grade it.

    If not, perhaps a clarifying question would help.....

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now