Help on read file!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;
using std::vector;

int main () {
  string line, tmp;
  vector <string> v;
  ifstream myfile ("file.txt");
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      v.push_back(line);
      getline (myfile,line);
      line += tmp;
      line +="\n";
    }
        cout << v[142] << endl;
    myfile.close();
  }else 
    cout << "Unable to open file"; 

  return 0;
}


Dear all currently my code only read by line.. I would like it to read line 142: [99] Hello // I want it to able to read the [99] part instead from line 142. How do I code it? Also it would be great if someone can teach me how to code from [99] to [126]. Thanks for helping me. Cheers!
help please :( cheers
bump this up
Not quite sure what you mean. Do you want to read this line [99] Hello, but only the [99] part?
> I would like it to read line 142: [99] Hello
> I want it to able to read the [99] part instead from line 142.

Assuming that line 142 is of this form: [nnn] xxxx and the number nnn is what you want to read:
1
2
3
4
5
std::istringstream stm( v[142] ) ;
char delimiter ;
int number ;
stm << delimiter // read the '['
       << number ; // read the number ; 


Oh! And do change
1
2
3
4
5
6
7
    while (! myfile.eof() )
    {
      v.push_back(line);
      getline (myfile,line);
      line += tmp;
      line +="\n";
    }


To
while ( getline (myfile,line) ) v.push_back( line + '\n' ) ;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* 
 * File:   main.cpp
 * Author: black4
 *
 * Created on January 13, 2012, 10:04 PM
 */
#include <istream>
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;
using std::vector;
using std::istringstream;

int main () {
  string line, tmp;
  vector <string> v;
  std::istringstream stm( v[99] );
  char delimiter;
  int number;
  ifstream myfile ("file.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) ) v.push_back( line + '\n' ) ;
    {
  stm << delimiter
          << number << endl;
    }
    cout <<  v[99] << endl;
    myfile.close();
  }else 
    cout << "Unable to open file"; 

  return 0;
}


Hi sorry, I still can't get the code right.

Anyway here are the sample content of the text which I would like to read.

line 142: [99] Hello my name is Bob
line 143: [100] Hello my name is sally [101] Hello
line 144: my name is Mark

I would like the output to be. the program is able to detect from [99] and read from that line after that read

Hello my name is Bob
Hello my name is sally
Hello my name is Mark

I also need the program to read from [99] all the way to [126]. Thanks and sorry for the trouble.

I manage to code it out using lines but if the textfile update through real time via -lynx dump line reading would be useless.
bump this up :(
a. let from = the position of the first "] " in the line
http://www.cplusplus.com/reference/string/string/find/

b. let to = the position of the next " [" after from (if any); end of string otherwise


c. extract the substring in ( from+2, to ]
http://www.cplusplus.com/reference/string/string/substr/
Last edited on
Topic archived. No new replies allowed.