read a text file

hello guys,

I am trying to read a text file which contains 5 blocks of 7 lines each, and each lines have different datatypes as follow:

*Beginning of text file*

string
string
string
string
int
int
double

string
string
string
string
int
int
double

string
string
string
string
int
int
double

string
string
string
string
int
int
double

string
string
string
string
int
int
double

*End of text file*

i would like to extract each line from the file then place it into a form. let's just say that the first line is a "Jesus" and i want to place it into a form where it is label Name, and so on; and perform a loop for each block of data.

for instance:

let's assume one block from the file is as follow.

Jeremia Atkins
Havard university
school of law
Les cayes
23
2342
198

and i want to place the data in a pre-existed form like this:

Name:
school:
major:
city:
age:
id:
weight:

and finally to output it like this:


Name: Jeremia Atkins
school: Havard university
major: school of law
city: Les cayes
age: 23
id: 2342
weight: 198

to perform this for each block in the text file.


thank you so much for helping me out with this.

some people said i might do functions but i have no idea.



P.S i tried to be as explicit as i could.


Try running this to see if it is to your liking.

PS: Normally we expect to see what work you've tried so far to solve this, but meh... I don't feel like teaching right now.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Example program
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

int main()
{
    // This is your file's text. Replace it with an std::ifstream fin("filename");
    // See: http://www.cplusplus.com/doc/tutorial/files/
    std::istringstream fin(
      "Jeremia Atkins\n"
      "Havard university\n"
      "school of law\n"
      "Les cayes\n"
      "23\n"
      "2342\n"
      "198\n"
      ""
      "Michael Waits\n"
      "Nilbog university\n"
      "school of goblins\n"
      "Nilbog\n"
      "11\n"
      "5342\n"
      "110\n"
    );

    std::string name;
    std::string school;
    std::string major;
    std::string city;
    int age;
    int id;
    int weight;
    
    // Ignore any initial empty lines
    // until we get to a valid name
    while (getline(fin, name) && name == "")
    {
        ;
    }
    
    while (getline(fin, school) && getline(fin, major) && getline(fin, city)
           && fin >> age >> id >> weight)
    {
         
        std::cout << "Name: "   << name   << "\n"
                  << "school: " << school << "\n"
                  << "major: "  << major  << "\n"
                  << "city: "   << city   << "\n"
                  << "age: "    << age    << "\n"
                  << "id: "     << id     << "\n"
                  << "weight: " << weight << "\n";
    
        // Ignore any empty lines between sets of entries
        // until we get to the next valid name
        while (getline(fin, name) && name == "")
        {
            ;
        }
        std::cout << "\n"; 
    }
}
thank you for replying Ganado, but unfortunately it didn't work. let me explain. this should be in the text file, and i just have to put the file name like infile("filetext.txt") without displaying it within the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// This is your file's text. Replace it with an std::ifstream fin("filename");
    // See: http://www.cplusplus.com/doc/tutorial/files/
    std::istringstream fin(
      "Jeremia Atkins\n"
      "Havard university\n"
      "school of law\n"
      "Les cayes\n"
      "23\n"
      "2342\n"
      "198\n"
      ""
      "Michael Waits\n"
      "Nilbog university\n"
      "school of goblins\n"
      "Nilbog\n"
      "11\n"
      "5342\n"
      "110\n"
    );


when i put the file name, it didn't extract the data from it, but if i copy the info from the text file and list it as you did, it will show it.

i should only have to write the file name like myfile("textfile") and the following code to match it
Yes, you need to replace std::istringstream with std::ifstream + proper constructor.
Read the tutorial I linked on files.

http://www.cplusplus.com/doc/tutorial/files/
http://www.cplusplus.com/reference/fstream/ifstream/ifstream/
https://stackoverflow.com/questions/7868936/read-file-line-by-line

PS: change int weight; to double weight; to match your original post.
Last edited on
Topic archived. No new replies allowed.