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.
// 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.
// 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