Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this is my code below, the output is numbers relating to a grade. I want to be able to make each line of the string a variable such as int grade1 and so on, so that I can display certain variables and not the whole string for example cout << grade1 << grade2 << endl;
using namespace std;
int main()
{
string line;
ifstream myfile("Data.csv"); //opening the CVS file
if (myfile.is_open())
{
while (getline(myfile, line)) //used to read each line of the file
{
string str(line); //making the file a string
char chars[] = ",abcdefghijklmnopqrstuvwxyzN;:/"; //Characters to be removed
for (unsigned int i = 0; i < strlen(chars); ++i)
{
str.erase(std::remove(str.begin(), str.end(), chars[i]), str.end());
}
str.replace(0, 9, " ");
cout << str << endl;
}
myfile.close(); //closes the file
}
else
cout << "pathway to file can't be found" << '\n'; //error message to display if file location cant be found.
cin.get();
return 0;
}
The question and especially the code is rather difficult to comprehend, but I understood that you want to create string variables for each line of the file.
A simple way is to loop through the file line-by-line, store the line into a string variable and then push that variable to a vector.
std::string line;
std::vector<std::string> lines;
while (getline(myfile, line))
{
lines.push_back(line);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I tried to open a certain file and read the content to put it into a string named letters but then every time I typed the file name I keep getting error can't find open the file.
void createTree()
{
BTS tree;
string filename;
string letters;
ifstream file;
int input = 0;
cout<<"Enter the file name: "; // ask the user to input the file name
cin >>filename; //input the user input into filename
file.open(filename.c_str());
if(file)
{
cout<<"File is open";
while(!file.eof())
{
cout<< "In the while loop"<<endl;
getline(file, letters);
tree.insertWord(letters);
}
}
else
cout<<"Error can't open the file"<<endl;
tree.printTree();
file.close();
}
Changed the getline call slightly, see if this works:
void createTree()
{
BTS tree;
string filename;
string letters;
ifstream file;
int input = 0;
cout<<"Enter the file name: "; // ask the user to input the file name
cin >>filename; //input the user input into filename
file.open(filename.c_str());
if(file)
{
cout<<"File is open" << endl;
ifstream file(filename.c_str());
char chars[100];
while (!file.eof()) {
file.getline(chars, 100);
tree.insertWord(letters);
letters = chars;
cout << letters << endl;
}
} else {
cout<<"Error can't open the file"<<endl;
}
tree.printTree();
file.close();
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is a home work question, so if you are not a fan of those I understand. Here is my code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
fstream myfile1("datafile1.txt"); //this just has a bunch of names in it
fstream myfile2("cmdfile1.txt"); //has commands like "add bobby bilbums"
ofstream outputFile("outfile1.txt"); //I want to take the "add bobby" command and copy the name into this new file.
string line;
if (myfile1.is_open() && myfile2.is_open()) //so I open both files
{
if (myfile2, line == "add"); //If myfile2 has an "add" in it
{
outputFile.is_open(); //open outputfile
outputFile << line << endl; //input the line with add in it till the end of that line.
}
}
cout << "\nPress Enter..."; // press enter and then everything closes out.
cin.ignore();
outputFile.close();
myfile2.close();
myfile1.close();
return 0;
}
Problem is, though the outputFile is always empty. It never copies any lines from cmdfile1 into the output file. Does anyone know what I am missing here?
Try something more like this instead:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream myfile1("datafile1.txt");
ifstream myfile2("cmdfile1.txt");
ofstream outputFile("outfile1.txt");
string line;
if (/*myfile1.is_open() &&*/ myfile2.is_open() && outputFile.is_open())
{
while (getline(myfile2, line))
{
if (line.compare(0, 4, "add ") == 0)
{
outputFile << line.substr(4) << endl;
}
}
}
myfile1.close();
myfile2.close();
outputFile.close();
cout << "\nPress Enter...";
cin.ignore();
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have written a file with 4 holiday destination and there prices , so i want to read the file and write destinations into the destination array and prices into the price array. the text format is like
suncity 250
ushakamarue 300
krugerPark 450
Tablemountain 340
this is what i have
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
//define a constant for the number of lines to read
#define NUM_READ_LINES 4
int main()
{
// array of line numbers each line being less or equal to 100 chars
char destination[NUM_READ_LINES][100];
//string price[30];
int counter = 0;
//ofstream outfile;
ofstream outfile("program.txt");
if(outfile.is_open())
{
outfile <<"suncity 250\n ";
outfile <<" ushakamarue 300\n";
outfile <<" krugerPark 450\n";
outfile <<" Tablemountain 340\n";
}
else
cout <<"Unable to open to file";
outfile.close();
//open a file
ifstream infile;
infile.open("program.txt");
if(infile.good())
{
//Read throuh file and load into array
while(!infile.eof() && (counter < NUM_READ_LINES))
{
infile.getline(destination[counter], 100);
counter++;
}
//loop hrough the array which we just put together
for (int i=0; i < counter;i++ )
{
cout << destination[i]<<endl;
}
}
infile.close();
return 0;
}
Currently the program is reading the whole line suncity 250 as destination instead of reading only suncity into destination[1] and 250 into Price[1].
Something like this:
string destination[NUM_READ_LINES];
string price[NUM_READ_LINES];
ifstream infile("program.txt");
for (int ii = 0; ii < NUM_READ_LINES; ++ii) {
infile >> destination[ii] >> price[ii];
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm reading in an input file with tab delimited Firstname, Lastname and zipcode. There are 25 of them. I'm trying to read it in, store it into objects and print it out again.
Here's the code:
// Reading in from an input file
ifstream inputFile("nameInput.txt");
string line;
for(int i = 0; i<25; i++){
while (getline(inputFile, line))
{
istringstream getInput(line);
string tempFName, tempLName;
int tempZip;
getInput >> tempFName >> tempLName >> tempZip;
// Creating new Person objects with the data
persons[i] = new Person(tempFName, tempLName, tempZip);
}
inputFile.close();
// Printing out String representation of the Person
persons[i]->toString();
}
While it compiles, during run-time this is the error I get: 87023
Segmentation fault: 11
Please help!!
The thing is, you need only one loop. This will read a maximum of 25 lines:
int main()
{
const int n_persons = 25;
std::shared_ptr<Person> persons[n_persons];
std::ifstream inputFile("nameInput.txt");
std::string line;
for(int i = 0; i < n_persons && std::getline(inputFile, line); ++i)
{
std::istringstream getInput(line);
std::string tempFName, tempLName;
int tempZip;
if(getInput >> tempFName >> tempLName >> tempZip)
{
// Creating new Person objects with the data
persons[i] = new Person(tempFName, tempLName, tempZip);
// Printing out string representation of the Person
persons[i]->toString();
}
else
cout << "error on line #" << i + 1 << " occurred" << endl;
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to read individual lines to different variables and I'm having trouble having it read anything past the first line
ifstream inputFileStream;
inputFileStream.open( fileName );
if (inStream.good() && inStream.open())
{
string empNum;
string name;
string streetAddress;
getline(inStream, empNum);
getline(inStream, name);
getline(inStream, streetAddress);
cout <<empNum << endl;
cout <<name << endl;
cout <<streetAddress << endl;
}
Also the txt file looks like this:
12
Bob
123 Main
555-555
This code works for me. copy & paste & run (without changes...)
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream inputFileStream("text.txt");
if (inputFileStream.good() )
{
string empNum;
string name;
string streetAddress;
getline(inputFileStream, empNum);
getline(inputFileStream, name);
getline(inputFileStream, streetAddress);
cout <<empNum << endl;
cout <<name << endl;
cout <<streetAddress << endl;
}
return 0;
}