Read columns from text files


 
Thread Tools Search this Thread
Top Forums Programming Read columns from text files
# 1  
Old 03-09-2014
Read columns from text files

Dear All,
I have basic structure of the C++ code [1]. It suppose to read particular columns from some txt file. The txt file look like following [1] (snippet). I have to ask the details for instance 'id' information for rows satisfying text with red color. The issue is that the txt file has not just the column wise data but other stuff as well, as you can check the [2]. Besides, the most important is I need to select data only for columns with [COLOR="rgb(255, 0, 255)"]phase[/COLOR] has certain value.
Thanks in advance,
emily,


ISSUEs facing:
a.) if there are top line with the
Code:
 number  side    id      id_2    phase   particulers     cost    phase

, it is acepting. Else if more than one such line is there, its not working.
b.) When I perform test with small file of some 500 lines, and tried to cout. It fail with
Code:
Segmentation fault (core dumped)

c.) The TASK is to plot
Code:
 id vs id_2

for certain condition being true, please check the BLUE text in the code
[1]
-----------------------------------------------------
Code:
#include <iostream>
#include <stdlib.h>
#include <string>
usign namespace std;

int main(int argc, char *argv[]){

int number[2600], side[2600] , id[2600]  , id_2[2600] , phase[2600] ,cost[2600], phase[2600],
string particulers[2600];
  string fileToOpen = argv[1];
  if(argc!= 1 || argc < 2) {
    cout<<"Error :check the no of input, require input.txt "<<endl;
    exit(0);
  }



  //open the file                                                                                                                                                               
  ifstream inFile(fileToOpen.c_str());
  if(!inFile.is_open())
    {
      cout << "Problem opening the file. Program terminating." << endl;
      exit(EXIT_FAILURE);
    }

  while(!inFile.eof()){
  inFile.ignore(numeric_limits<streamsize>::max(), '\n');
  inFile >> number[i] >> side[i] >> id[i] >> id_2[i] >> phase[i] >> particulers[i] >> cost[i] >> phase[i] ;
    if (particulers[i].compare("top") == 0 && phase[i] == 0 ){
  //  PLOT (id vs id_2) 
    }
      i++;
  }
  i--;
  }

}



[2]
--------------------------------------------------
Code:
number  side    id      id_2    phase   particulers     cost    phase
-1      20      98      9       87      HO001           725    0
-1      50      98      10      87      HO002           725     0
-1      30      97      9       90      HO003           728     0
-1      30      97      9       90      HO003           728    0
-1      20      98      15      87      HO001           725    1
-1      50      98      16      87      HO002           725    0
-1      30      97      19      90      HO003           728    1
-1      30      97      20      90      HO003           728    1

number  side    id      id_2    phase   particulers     cost    phase
-1      20      98      45      87      HO001           730    1
-1      50      98      80      87      HO002           745    1
-1      30      97      90      90      HO003           759    1
-1      30      97      50      90      HO003           739    1


Last edited by emily; 03-10-2014 at 05:25 AM.. Reason: modified the code and asked few particular questions
# 2  
Old 03-09-2014
So, you want the user to enter the ID and then based on the ID display the particulars and cost? Or user will chose to enter what he/she wants?
# 3  
Old 03-10-2014
Hi ahamed,
I want to plot two variables for certain condition being true. I will decide it itself in the code for which conditions I would like to plot the variables for.
Please check the modified code and TASK that I have added in the original message.
The file is really huge and it so far not working.

Thank you,
emily,
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate columns into different text files

Hi I have large text file consisting of five columns. Sample of the file is give below: ed 2-4 12.0 commons that they depended on. मानवों नष्ट किया जिन पर वो आधारित थे। ed 3-1 12.0 Almost E, but would be over. रचना करीब करीब ई तक जाती है, मगर तब तो नाटक ख़त्म हो... (2 Replies)
Discussion started by: my_Perl
2 Replies

2. Shell Programming and Scripting

Comparing columns in 2 text files

Hi i have 2 files file1.txt XX,ZZ,XC,EE,RR,BB XC,CF,FG,RG,GH,GH File2.txt DF,GH,MH,FR,FG,GH,NOTOK XX,ZZ,XC,EE,RR,BB,OK result XX,ZZ,XC,EE,RR,BB OK look for column1 , XX and if it matches in File2.txt , retrieve the 7 th field from File2 and print in 3 rd file , ... (9 Replies)
Discussion started by: Shyam_84
9 Replies

3. Shell Programming and Scripting

Read text between regexps and write into files based on a field in the text

Hi, I have a huge file that has data something like shown below: huge_file.txt start regexp Name=Name1 Title=Analyst Address=Address1 Department=Finance end regexp some text some text start regexp Name=Name2 Title=Controller Address=Address2 Department=Finance end regexp (7 Replies)
Discussion started by: r3d3
7 Replies

4. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

5. UNIX for Dummies Questions & Answers

Merging two text files by two columns

Hi, I have two text files that I would like to merge/join. I would like to join them if the first columns of both text files match and the second column of the first text file matches the third column of the second text file. Example input: First file: 1334 10 0 0 1 5.2 1334 12 0 0 1 4.5... (4 Replies)
Discussion started by: evelibertine
4 Replies

6. UNIX for Dummies Questions & Answers

Combining two text files as columns?

I have one space delimited file with multiple columns and one tab delimited file with multiple columns (They have the same number of rows). I want to basically combine these two text files into a new text file by column. How would I go about doing that? (1 Reply)
Discussion started by: evelibertine
1 Replies

7. Shell Programming and Scripting

Can I use columns from text files in if statements?

Currently I am stuck on my program for my if statement, as I do not no how to get a specific collumn of text to check against my variable. df -k /home/* > fd.txt cat df.txt | tr -s " " | awk '{print $3, $4, $1}' > dfcap.txt echo -n "Enter the size (in kilobytes) you wish to check against... (18 Replies)
Discussion started by: ninjafish
18 Replies

8. Shell Programming and Scripting

read input from 2 files and print them in alternate columns

Hi Frnz Please help me out. I have two text files. A.txt one two three four B.txt Jan Feb Mar Apr I need the output as (1 Reply)
Discussion started by: sriram.s
1 Replies

9. Shell Programming and Scripting

merging few columns of two text files to a new file

hi i need to select a few columns of two txt files and write it to a new file. there is one common field for both of these files. plz help me in this thanks in advance (4 Replies)
Discussion started by: kolvi
4 Replies

10. UNIX for Dummies Questions & Answers

searching text files on specific columns for duplicates

Is it possible to search through a large file full of rows and columns of text and retrieve only the rows that contain duplicates fields, searchiing for duplicates on col4 & col6 Sample below Col1 col2 col3 col4 col5 col6 G405H SURG FERGUSON ... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question