Thread: How to delete every other line in a text file?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    2

    How to delete every other line in a text file?

    I haven't done C++ in ages, and I need a program that can delete every other line in a text file.

    I need it because I'm analyzing hex code in a text file which outputs data in this fashion:

    sent hex code
    received hex code
    sent hex code
    received hex code
    sent hex code
    received hex code
    etc.

    I need to delete the received hex code, and just be left with the sent hex code.

    Can anyone help me out? I'd really appreciate it!

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Very simple.
    Code:
     std::string line;
     for(;;)
     {
       std::getline(inFile, line);
       if(inFile.eof())
    	  break;
       outFile << line << std::endl;
     
       inFile.ignore(inFile.rdbuf()->in_avail(), '\n');
     }
    Read a line, write it, ignore the next line. Repeat until EOF
    Last edited by Hunter2; 01-20-2005 at 01:16 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    2
    when i try to compile it, i get this error message:

    Code:
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    C:\Borland\BCC55\delete.cpp:
    Error E2090 C:\Borland\BCC55\delete.cpp 1: Qualifier 'std' is not a class or namespace name
    Error E2040 C:\Borland\BCC55\delete.cpp 1: Declaration terminated incorrectly
    Error E2040 C:\Borland\BCC55\delete.cpp 2: Declaration terminated incorrectly
    Error E2040 C:\Borland\BCC55\delete.cpp 2: Declaration terminated incorrectly
    *** 4 errors in Compile ***

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    you have some sort namespace or other conflict/syntax error which will require that you post your code for a more specific answer.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Very likely, you're either including <fstream.h>. Try including <fstream> instead, the .h version is either deprecated or nonstandard (either way, you shouldn't use it). Failing this, post some of your code.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The previous example actually has some subtle bugs that we might want to fix.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <limits>
    
    bool skiplines(std::istream &is, std::ostream &os) {
        std::string line;
        while(std::getline(is,line)) {
            if(!(os << line)) return false; 
            if(!is.eof() && !(os << '\n')) return false;
            is.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
        }
        return is.eof();
    }
    
    int main(int argc, char *argv[]) {
        using namespace std;
        const char *iname = (argc>1)?argv[1]:"skiplines.cpp";
        string oname = (argc>2)?argv[2]:iname+string(".out",4);
        ifstream ifs(iname);
        ofstream ofs(oname.c_str());
        if(skiplines(ifs,ofs)) {
            cout << "Done!" << endl;
        } else {
            if(!ifs) cerr << "Error processing file " << iname << endl;
            if(!ofs) cerr << "Error processing file " << oname << endl;
        }
        return 0;        
    }
    Ok so this is a bit overboard, but the main point is where I test eof() in the above. A file that has failed for any reason will never reach eof() Thus you check eof after you are done reading to test if the reason you stopped was that your ran out of things to read. eof() has one other subtle use, an that is when getilne reads a line that goes to the end of file, but that does not contain a newline eof is true, but fail is false. It's a perfectly good line, but the previous example bailed out too early. Testing the state of the output stream after every operation is just me being paranoid, but its a good idea to check up how your output is going. in_avail() is the number of bytes in the buffer, not the file, though it could very easily be the total file size. numeric_limits is a good example of how Stroustrup must be getting a kickback from some keyboard manufacturers. Nevertheless it's a good generic way to find things like the largest possible number of whatever type. streamsize is probably size_t, and size_t is probably unsinged long, but this can change and I don't need to know about any of it, because I know about numeric_limits.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    eof() has one other subtle use, an that is when getilne reads a line that goes to the end of file, but that does not contain a newline eof is true, but fail is false.
    Right, my mistake. Dealing with EOF with/without a newline at the end of the file is just about my #1 pet peeve -_-
    in_avail() is the number of bytes in the buffer, not the file, though it could very easily be the total file size.
    You're right. I was thinking of using numeric_limits, but since in_avail() works for cin at the end of a program (I rarely use ignore() for anything else), I wrongly assumed that it would work for general use in file streams as well.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. delete line from text file
    By Waldo2k2 in forum Linux Programming
    Replies: 6
    Last Post: 05-07-2005, 06:03 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM