CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2010
    Posts
    8

    Question Text reading and writing

    Hi -
    I'm trying to seperate out some numbers from a large text file and place those numbers in a formated fashion inside another text file. I'm working in MS Visual C++. The file i'm reading from has lines line the following:

    AT BUS [CALN HHG000 138.00] 77904 AREA 7 (KV L-G) V+: / 0.000/ 0.00 (KV L-G) VA: / 0.000/ 0.00 V0: / 42.250/ 178.86
    THEV. R, X, X/R: POSITIVE 0.03296 0.13488 4.093 NEGATIVE 0.03297 0.13493 4.092 ZERO 0.08720 0.30104 3.452

    I need the 77904 from the first line and the first two numbers after "POSITIVE" and "ZERO" on the second line. This format of text is always the same because a computer program generates a large .txt file with all these numbers in it and rather than sort through it and copy the numbers by hand, i'd rather write a text file that grabs those fields i need and shoves them into a txt file seperated by commas or tabs so that i can easily import them into Excel. Here's my code:

    #include <iostream>
    #include <fstream>
    #include <string>

    using namespace std;

    int main(int argc, char* argv[]){
    char* inFile;
    char* outFile;
    string line;
    int firstMarkPos = 0;
    int secondMarkPos = 0;
    char busNum[] = "";

    //MARKER DECLARATIONS!!!
    /******************************************/
    /**/ char firstMarker[] = "]"; /**/
    /**/ char secondMarker[] = "AREA"; /**/
    /******************************************/

    if(argc < 3){//Check argc. If too few arguments inform user of syntax.
    cout << "Syntax: text_parser.exe <readFile> <writeFile>" << endl;
    return 1;
    }else{//if you got enough parameters

    cout << argv[0] << endl;//for debug purposes. Remove later.

    //inFile and outFile point to the beginning of the char array that contains
    //the user's commands
    inFile = argv[1];
    outFile = argv[2];
    }

    //open the file the user specifies and copy the text from the input file...
    ifstream inputF;
    ofstream outputF;
    inputF.open(inFile);
    outputF.open(outFile);

    while(!inputF.eof()){
    getline(inputF,line);//grab a line from the text file
    firstMarkPos = line.find(firstMarker);
    secondMarkPos = line.find(secondMarker);

    if(firstMarkPos != string::npos && secondMarkPos != string::npos){//If both markers are detected...

    //copy the string inbetween the markers to the character array, busNum...
    line.copy(busNum,secondMarkPos - (firstMarkPos + 1),firstMarkPos + 1);

    //THE CHARACTER ARRAY BUSNUM MAY NOT HAVE A NULL CHARACTER '\0' AT THE END!
    for(int i = 0; i <= strlen(busNum);i++){
    if(busNum[i] == ' '){
    //do nothing
    }else{
    outputF << busNum[i];//possibly wrong
    }
    }

    outputF << "\n";

    }else{
    //only one marker or no markers detected
    }
    //cout << firstMarker << " " << firstMarkPos << endl;
    //cout << secondMarker << " " << secondMarkPos << endl;
    //cout << line << endl;//to the screen...
    //outputF << line << endl;//and to the file!
    }

    inputF.close();
    outputF.close();//close both files

    system("pause");//allow user to see that the program is complete before exiting

    return 0;
    }

    Thanks for your help!

  2. #2
    Join Date
    Jul 2010
    Posts
    8

    Re: Text reading and writing

    Sorry I forgot to mention what was happening at execution. I execute from the cmd prompt and a window pops up saying that my program: "text_parser.exe" needs to close. If that helps you at all...

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Text reading and writing

    1. Edit your OP adding Code tags around code snippet.
    2. Debug your code (use F5 to start debugger) and then go on line by line (F10) to see what happens
    Victor Nijegorodov

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Text reading and writing

    You have ...
    Code:
    char busNum[] = "";
    
    // then you do the folllowing :
    
    line.copy(busNum,secondMarkPos - (firstMarkPos + 1),firstMarkPos + 1);
    busNum is not large enough to hold any characters. Why not just
    use std::substr() and place into a std::string ?

  5. #5
    Join Date
    Jul 2010
    Posts
    8

    Smile Re: Text reading and writing

    Philip -
    Thanks for your help. Perhaps that is a good idea. That part of the code is meant to grab the number associated with a power bus (I'm an intern working for a power distribution company) by reading through a Report File generated by some in-house software. I took a look at the format of this report and noticed that the bus number always appeared between the character ']' and the word 'AREA'. My intention with this code was to scan a line from the report file into memory and have the computer run through it looking for these characters in the correct order. Since they don't occur on other lines paired together like the lines that contain the bus number, I could safely have the computer ignore the lines where only one or neither of these 'markers' occur. When both markers are found i wanted the program to grab the string in between the two location of the markers and dump it into a char array and strip away the white space and throw the remaining characters into a file, to which the program would append some other numbers i wanted to grab from the report as well, but for now, i'm just trying to tackle grabbing the bus numbers. As to your suggestion: If i loaded the string in between ']' and 'AREA' using std::substr (from the example in my OP this string would read: "_77904_" where the underscores represent whitespace) could i remove that whitespace?

    Again, thanks for all your help!

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Text reading and writing

    You can remove leading and trailing whitespaces using a combination
    of string::erase and string::find_first_not_of (leading) or string::find_last_not_of
    (for trailing).

    Since you are going to want numbers ... you might consider using stringstream ...

    Code:
    string s;  // which contains the substr()
    
    int value;
    
    stringstream ss(s);
    ss >> value;
    
    // and in the second part ...
    
    float f1,f2;
    
    stringstream ssf(s);
    ssf >> f1 >> f2;

  7. #7
    Join Date
    Jul 2010
    Posts
    8

    Smile Re: Text reading and writing

    Thanks a lot for your help Philip! I think i figured it out.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured