Question

In: Computer Science

C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...

C++ Parse text (with delimiter) read from file.

If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector.

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

class Student{
    public:
        string name;
        int age;
};

int main() {


    vector <Student>studentVector;
    Student s;
    string student_name;
    int student_age;
  
    ifstream myFile("file.txt");
      
    while(myFile>> student_name >> student_age){


        //s.name is assigning the entire line of unparsed text
        s.name = student_name;

       ///age is not read because input string from file is not parsed

        s.age = student_age;


        studentVector.push_back(s);
    }


    return 0;
}

Solutions

Expert Solution

working code :

#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

class Student{
public:
string name;
int age;
};

int main() {
vector <Student>studentVector;
Student s;
string student_details;
ifstream myFile("file.txt");

while(myFile>> student_details){
int index_of_comma = student_details.find(','); // finding the index of comma(',')
  
s.name = student_details.substr(0,index_of_comma);// get the string from 0 to index_of_comma and store ot to s.name
  
s.age = stoi(student_details.substr(index_of_comma+1,student_details.size())); // form index_of_comma+1 to the last stroe it in s.age
  
// stoi is used to convert the string of number into integer
cout<<"student name is :"<<s.name<<"\nstudent age is :" <<s.age<<endl; // printing values

studentVector.push_back(s); // inserting details of student into vector
}


return 0;
}

if any doubt, comment below


Related Solutions

C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has...
(C++) Write a program to read from a grade database (data.txt). The database (text file) has students names, and grades for 10 quizzes.Use the given function prototypes to write the functions. Have main call your functions. The arrays should be declared in main() and passed to the functions as parameters. This is an exercise in parallel arrays, int and char 2 dim arrays. Function prototypes: int readData(ifstream &iFile, int scores[][10], char names[][30]); This functions takes the file stream parameter inFile...
In C++, write a program that accepts a text file of ASCII words from standard input...
In C++, write a program that accepts a text file of ASCII words from standard input and store them and the amount of times the word appears in the file in a hash table using external chaining. Then print the words and their counts sorted based on alphabetical order and print them again in decreasing numerical order based on the amount of times the word appears in the file. Space, tab, and new line all count as space characters. The...
C++ Question 2 You will read in data about the planets from a text file, and...
C++ Question 2 You will read in data about the planets from a text file, and then print out the data about that planet when the user requests the data. You will need to create a Planet Class (since this assignment does not cover templates, you are expected to split your class into .cpp and .h files), as well as a .cpp/.h library with the functions described below Planet Class For the first phase of the question, you will create...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
In this lab, you open a file and read input from that file in a prewritten...
In this lab, you open a file and read input from that file in a prewritten C++ program. The program should read and print the names of flowers and whether they are grown in shade or sun. The data is stored in the input file named flowers.dat. Instructions Ensure the source code file named Flowers.cpp is open in the code editor. Declare the variables you will need. Write the C++ statements that will open the input file flowers.dat for reading....
For c language. I want to read a text file called input.txt for example, the file...
For c language. I want to read a text file called input.txt for example, the file has the form. 4 hello goodbye hihi goodnight where the first number indicates the n number of words while other words are separated by newlines. I want to store these words into a 2D array so I can further work on these. and there are fewer words in the word file than specified by the number in the first line of the file, then...
how to count the word of occurance in the text file example input text file :...
how to count the word of occurance in the text file example input text file : "test1.txt hello : 1 good : 1 morning: 1 how : 1 are : 2 you : 2 good : 1 example output text file : "test2.txt" 1 : 4 2 : 2 3 : 0 4 : 0 5 : 0
Write a C program to run on unix to read a text file and print it...
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring] filename • The -c flag...
using java, parse a text file to be able to list the word(s) with the highest...
using java, parse a text file to be able to list the word(s) with the highest frequency in a sentence across all sentences in the whole file, also print its frequency and the corresponding sentence. cannot use hash maps. assume text file will be multiple paragraphs long.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT