Help - Output specific data from 2 txt files

This is a discussion on Help - Output specific data from 2 txt files within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Im sort of new to coding and need help creating a program that will help me with somthing ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    Help - Output specific data from 2 txt files

    Hi guys, Im sort of new to coding and need help creating a program that will help me with somthing that I'm currently working on. My knowledge is extremely basic so I was only able to create a very simple form of what I actually need. Im sure there is better ways of doing this, but this is what I was able to do through quickly reading tutorials online. The code only does the first part of what I need it to do - it reads a file and then lists the model names to a seperate file. Here is the source code to what I currently have:

    Code:
     #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        ifstream in("datain.txt");
            short int a1;
            string a2,a3,a4,a5,a6,a7,a8,a9,a10,a11;
        ofstream out("dataout.txt");
    
        while(1)
        {
            if((in>>a1>>a2>>a3>>a4>>a5>>a6>>a7>>a8>>a9>>a10>>a11))
            {
                out<<a2<<endl;
            }
            else
                break;
        }
    
        return 0;
    }
    Basically I need a program that looks through 2 different .txt files and outputs specific data from these files into a seperate file..

    Sample input from text file:
    (id, modelname, int, x, y, z, rotx, roty, rotz, int, LODindex) <- this part is not in the file.
    # countn2_stream0.ipl
    16683, cn2_alphabit01, 0, -426.523, 558.133, 16.352, 0.000, 0.000, -0.995, 0.098, -1
    3336, cxrf_frway1sig, 0, -385.844, 547.844, 15.570, 0.000, 0.000, -0.996, 0.087, -1
    700, sm_veg_tree6, 0, -436.711, 600.969, 16.172, 0.000, 0.000, 0.000, 1.000, -1
    16224, s_bit_14, 1024, -428.641, 604.625, 1.961, 0.000, 0.000, 0.000, 1.000, 42
    .........................
    .........................
    1294, mlamppost, 256, -280.383, 1123.414, 23.203, 0.000, 0.000, -1.000, -0.009, -1
    16006, ros_townhall, 0, -215.234, 1119.195, 18.352, 0.000, 0.000, 0.000, 1.000, 37
    669, sm_veg_tree4, 256, -225.312, 1127.211, 18.805, 0.000, 0.000, 0.000, 1.000, -1
    1294, mlamppost, 0, -199.672, 1123.555, 23.203, 0.000, 0.000, -1.000, -0.000, -1
    1468, DYN_MESH_05, 256, -232.758, 1129.758, 19.938, 0.000, 0.000, -1.000, 0.000, -1
    1468, DYN_MESH_05, 256, -227.484, 1129.758, 19.938, 0.000, 0.000, -1.000, 0.000, -1
    1294, mlamppost, 0, -186.461, 1123.398, 23.203, 0.000, 0.000, 0.000, 1.000, -1
    16070, des_stwnhotel1, 0, -174.211, 1120.453, 24.406, 0.000, 0.000, 0.000, 1.000, 38
    I need the modelname part to be output to a list. If the last number of the line is -1 then ONLY the model name is needed. However, if the last number of the line is anything other than -1 then that number should be stored as it relates to a line number in the next file that will be read. So form the input above most of the lines have -1, but ros_townhall line ends with 37, so in this next file I need the program to go to line 37 in that file and store the model name on that line. I've been having difficulty finding how to get the program to go to a specific line, without the lines actually being numbered. When looking through the 2nd file, the program should skip over lines which start with "#" as these are just comments and should be ignored. The program should also skip over lines that contain 1 word such as "inst" or "objs"..


    Sample input from the 2nd txt file that is read:

    Line # < not part of the file just a comment by me. The actual file also does not have the lines numbered.

    (this line is NOT COUNTED) inst
    0 - 16265, des_damlodbit04, 1024, -606.0859375, 1910.101563, 9.140625, 0, 0, 0, 1, -1
    1 - 16273, oilderricklod01, 0, 422.9140625, 1513.414063, 10.71875, 0, 0, 0, 1, -1
    2 - 16274, oilderricklod02, 0, 435.0546875, 1269.6875, 8.6953125, 0, 0, 0, 1, -1
    ............................
    ............................
    ............................
    35 - 3683, lod_indust1las, 0, 133.7421875, 1459.640625, 17.09375, 0, 0, 0, 1, -1
    36 - 3289, lod_refthinchim1, 0, 207.5390625, 1371.242188, 9.5859375, 0, 0, 0, 1, -1
    37 - 16412, lod_cn2townhall, 0, -215.234375, 1119.195313, 18.3515625, 0, 0, 0, 1, -1
    38 - 16413, lod_stwnhotel1, 0, -174.2109375, 1120.453125, 24.40625, 0, 0, 0, 1, -1


    Output should be a list like this:

    Normal, Associated LOD
    cn2_alphabit_01,
    cxrf_frway1sig,
    ...............
    ...............
    rostownhall, lod_cn2townhall
    des_stwnhotel1, lod_stwnhotel1
    ...and so ON..
    I just need all of the model names and their associated LOD names from these files. What would be the best way to achieve this? Thanks in advance to any help I receive.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You could read the relevant data from the second file into a vector and then index the vector with the number at the end of the first file's records (unless it's -1).
    A quick-and-dirty example:
    Code:
        string str;
        vector<string> lod;
        ifstream ifs("second_file.dat");
        while (getline(ifs, str)) {
    
            // Skip any line that contains a #
            size_t comm = str.find_first_of("#");             if (comm != string::npos) continue;
    
            // Skip any line that does not contain a comma (this will skip the single words)
            size_t name1 = str.find_first_of(",");            if (name1 == string::npos) continue;
    
            name1 = str.find_first_not_of(" ", name1 + 1);    if (name1 == string::npos) continue;
            size_t name2 = str.find_first_of(", ", name1);    if (name2 == string::npos) continue;
    
            lod.push_back(str.substr(name1, name2 - name1));
        }
        ifs.close();
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specific data extraction from .txt files using fstream.h
    By xScen3xHdstyl3x in forum C++ Programming
    Replies: 4
    Last Post: 10-28-2011, 01:58 AM
  2. How to read a specific content and output in differ way?
    By cr7tiano in forum C++ Programming
    Replies: 1
    Last Post: 07-06-2010, 01:51 AM
  3. Getting Specific Data from a file
    By darknite135 in forum C++ Programming
    Replies: 4
    Last Post: 12-31-2007, 10:41 PM
  4. Entering specific data
    By fantim in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:47 PM
  5. Help with specific output function
    By indigo0086 in forum C++ Programming
    Replies: 3
    Last Post: 11-19-2002, 09:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21