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++) 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...
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...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
Complete the program to read in values from a text file. The values will be the...
Complete the program to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows) and the number...
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.
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT