Find the answer to your Linux question:
Results 1 to 4 of 4
i am new here and i am learning programming now.I am trying to write a program usin c++ on linux machine.The program reads the file "originfile"which has tab separated data, ...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    19

    Angry reading a string in a file and remove white space

    i am new here and i am learning programming now.I am trying to write a program usin c++ on linux machine.The program reads the file "originfile"which has tab separated data, then it removes extra white space(tab in this case) and output the data to file "editedfile" .The program looks like this

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <cstdlib>

    using namespace std;
    void check_blank(ifstream& instream, ofstream& outstream)

    int main()
    {
    ifstream instream;
    ofstream outstream;
    bool t;
    string num;

    instream.open("originfile.txt"); //Open file: text to be edited
    if(!instream) //Make sure file opened successfully
    {
    cerr << "Error:File couldnt open1st";
    exit(1);
    }
    outstream.open("editedfile.txt"); //Open file: edited text will go
    if(!outstream.fail()) //Check if open failed
    {
    t = true;
    check_blank(instream, outstream); //Call function to input edited text into file
    }
    else
    {
    cerr << "File failed to open (2nd)\n";
    exit(1);
    }
    instream.close(); //Close both files
    outstream.close();

    return 0;
    } //End main

    void check_blank(ifstream& instream, ofstream& outstream)
    {

    string word;
    //Variable tells if there are multiple blank spaces

    while(instream >> word) //While the file isn't at the end
    {
    outstream << word << " " << endl;
    }
    }
    *******************************************

    The originfile looks like this(separated by tab space)

    input1 output1
    input2 output2
    input3 output3

    and editedfile i get after compiling the program looks like
    input1 output1 input2 output2 input3 output3

    My problem is i would like the editedfile to look like this(separated only by one space)
    input1 output1
    input2 output2
    input3 output3

    Can anybody please tell me how i can fix this. thank you

  2. #2
    Just Joined!
    Join Date
    Aug 2005
    Posts
    65
    well i donno much about that problem but i think u must make 2 loops
    the upper one is to read all the lines ,
    while( file.eof() )
    {
    read line into strings;
    }

    the inner loop is to iterate on the string of the whole line and parse word by word
    to do this u can use stringstream or make manual thing..

    that is a method , but u can use another one not using the operatr ">>"
    since this operator absorb the white space and the end of line

  3. #3
    Linux Newbie
    Join Date
    Mar 2008
    Location
    Hyderabad
    Posts
    109
    Make change like this

    void check_blank(ifstream& instream, ofstream& outstream)
    {
    char word;
    //Variable tells if there are multiple blank spaces

    while(instream >> ch) //While the file isn't at the end
    {
    if(ch =='\t')
    outstream << ' ';
    else
    outstream <<ch;
    }
    }
    might work

  4. #4
    Just Joined!
    Join Date
    Dec 2008
    Posts
    19

    Thank you guys

    thanks for your replies....were very helpful and now the problem is solved....till then.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...