C++ Input File line by line


 
Thread Tools Search this Thread
Top Forums Programming C++ Input File line by line
# 1  
Old 04-09-2013
C++ Input File line by line

Hi there, I need to read some data from a file with string and number that is similar to this:
Code:
           
   word1 0.0 1.0 0.0
   word3 word4 0.0 0.0 -1.0
   word1 0.0 0.0 1.0
   word5

With this code:
Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
    {
      double d1, d2, d3;
      string str1, str2;
      int count=0;
      ifstream infile("file.dat");
      while(infile>> str1 >> d1 >> d2 >> d3 )
          {
          count++;
          cout<<str1<<" " << d1 <<" " <<  d2 <<" " << d3 <<endl;
        }
     cout<<"There are "<<count<<" lines with the string: "<<str1<<endl;
     return 0;
      }

I need to use the linux comand grep to select all the lines with word1 and then I can get the values from file, but I would like to know if I can get the same result within C++ code (i.e. select lines with word1 and then those with word 3 word4)
Thanks in advance!
# 2  
Old 04-09-2013
without knowing what you did with grep it's hard to see what to do with C/C++...

In its simplest form you can do this to print matching lines:

Code:
#include <stdlib.h>
#include <string.h>

int main()
{
        char buf[1024];

        while(fgets(buf, 1024, stdin) != NULL)
        {
                if(strstr(buf, "word1") == NULL) continue;
                fputs(buf, stdout);
        }
}

Code:
./program < inputfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-09-2013
I used grep to select all the lines with word1:
Code:
grep word1 file.dat > filenew.dat

but I am sure that there more elegant way with C++.
# 4  
Old 04-09-2013
See the program above, which matches all lines containing 'word1' and prints them back out.
# 5  
Old 04-10-2013
Thanks Corona688, but with your code how could I assign the values to string str1 and double d1, d2, d3? As in the previous code:
Code:
infile>> str1 >> d1 >> d2 >> d3

# 6  
Old 04-15-2013
I'm not sure that code ever worked for one thing. cin, like scanf, has troubles with newlines plugging up the pipeline... using sscanf avoids that.

Code:
int main()
{
        char buf[1024];

        while(fgets(buf, 1024, stdin) != NULL)
        {
                char word1[64];
                double d1, d2, d3;
                if(strstr(buf, "word1") == NULL) continue;
                sscanf(buf, "%s %f %f %f", word1, &d1, &d2, &d3);
                printf("got word %s d1 %f d2 %f d3 %f\n", word1, d1, d2, d3);
        }
}


Last edited by Corona688; 04-15-2013 at 03:42 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Curl - input line by line from text file

Hi, I've got a text file with hundreds of lines I need to upload to an API via curl, one by one. The text file is like: 2012-08-01 10:45,124 2012-08-02 10:45,132 2012-08-03 10:45,114 I want to get curl to go through the text file sending a post for each line. like: curl --request... (0 Replies)
Discussion started by: emdeex
0 Replies

4. Shell Programming and Scripting

Input file line matching

Hi there, I have two input files. I need to match the last two columns of each line in File1 with the first two columns of a line in File2. Example: File1 AAA BB 234 789 BBB CC 426 624 CCC DD 356 643 File2 766 332 12 234 789 64 122 633 23 426 624 88 777 453 22 356 643 92 ... (3 Replies)
Discussion started by: palex
3 Replies

5. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

6. Shell Programming and Scripting

sed to read line by line and input into another file

I have two files. Fileone contains text string one text string two text string three Filetwo contains Name: Address: Summary: Name: Address: Summary: Name: Address: Summary: I would like to use sed to read each line of file one and put it at the end of the summary line of file... (3 Replies)
Discussion started by: dolacap
3 Replies

7. Programming

How to take input from cmd line into file

Hi, I want to be able to write a simple program that takes in input from the command line. I;m am at the level of getchar and putchar. Any examples would be a great help thanks. I intend/prefer also to use the pipe command | eg: input | file1 ---------- Post updated at 04:08 PM ----------... (4 Replies)
Discussion started by: metros
4 Replies

8. Shell Programming and Scripting

Input 2 files, calculate diffs line by line

Hi I am new to Unix and need help with the following (to you all I'm sure) simple task. I am trying to output the differences between previous snaphots of various filesystem sizes to the present sizes. I have three files (e.g.) : file 1 file 2 file 3 10 100 /var... (4 Replies)
Discussion started by: bigbuk
4 Replies

9. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies
Login or Register to Ask a Question