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
Non-working code:
#include<iostream>
#include<fstream>
#include<string>
int main(){
int id; string name;char comma ; double money;
ifstream read("testfile.csv");
while (read >> id >> comma>> name >> comma >> money)
{cout << id <<comma<<name<<comma<<money<< endl ;}
read.close();
_getch();
return 0;}
The csv file data & structure:
1,user1,999
2,user2,33
3,user3,337
But, the following works fine. Why so?
while (read >> id >>comma>>name)
{cout << id<<comma<<name <<endl ;}
When you read a string using >>, it reads a space delimited string. If there is no space in the text you read, it will read until the end of the line (as newline is a space).
Because of this the "parsing" of the input will after a little while be out of sync with the contents from the file, and will lead to an error when attempting to read one of the numbers.
Related
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 days ago.
Improve this question
int main()
{
string array[4000];
short loop = 0;
string line;
ifstream myfile("out0.txt");
if (myfile.is_open())
{
while (!myfile.eof())////
{
getline(myfile, line);
array[loop] = line;
cout << array[loop] << endl;
loop++;
}
myfile.close();
}
else cout << "can't open the file";
}
I have a txt file which contains data as:
12.4
45.2
65.2
44.3
...
...
...
I want to read this column in an array. Above is the code but its not giving the desired output kindly help
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 3 years ago.
Improve this question
I am trying to read values from a .txt file to a vector (which is a member of a class) in C++, but despite the .txt having around 1000 lines, the vector is of size 0. I inserted a 'cout', and I know the file is opened and closed. I'm not sure what I could be doing wrong for the code not to read the contents of the .txt.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include "option_class.h"
using namespace std;
int main(){
double cprice = 0.0;
int i = 0;
string line;
ifstream is;
is.open("/Users/<USER>/Desktop/SPY.txt");
if (!is){
cout << "Unable to open file" << endl;
return(0);
}
while(!getline(is, line).eof()){
is >> cprice;
option1.price.push_back(cprice);
}
is.close();
cout << "Closing file" << endl;
}
Have you tried something simpler:
while (is >> cprice)
{
option1.price.push_back(cprice);
}
The operator>> will skip whitespace, which includes newlines. There is no need to read a line at a time.
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 5 years ago.
Improve this question
Sir, I am creating my university project on bank database system on dev c++(object-oriented paradigm). So, I want to enter the data in c++ and wanted to save the data in the notepad file.
For Example:
Dev c++ :
#include <iostream>
#include <cstdlib>
using namespace std;
int main(){
int a;
cout<<"Enter a number"<<endl;
cin>> a;
}
:- When the user enters a number in the console of Dev C++, then the output will be saved in notepad. How will it be??
Sorry.... for unclear question...
Yes, Suraj Roa you got my logic. that show the user input number in .txt file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int a;
cout << "Enter number : " << endl;
cin >> a;
// open file stream
ofstream file;
file.open("number.txt");
file << a;
file.close();
return 0;
}
You need to create a stream to external file and then you can use it like std output stream (cout).
All you need to understand this mechanism you can find here.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using the following code to save the array data into file
ofstream out;
out.open("Database.txt", ios::trunc);
for(int t=0; t<Data_Counter; t++)
{
out << t <<endl;
out << Data[t].getName() << endl;
out << Data[t].getID() << endl;
}
out.close();
now I want to retrieve the data back in the same array positions. What do I need to add in the following code to make it work:
ifstream in;
in.open("\Database.txt", ios::in);
// what logic to use
in.close();
To read to the end of your file use some code like this
int index;
string name, id;
while (in >> index >> name >> id)
{
// do something with index, name and id
}
This works because in >> ... is only true when the read was successful. So when you get to the end of the file in >> ... will be false and the while loop will terminate.
I am assuming that there's no whitespace in your names and ids. If that's not the case then you have a slightly more difficult problem.
Another way is to take advantage of the fact that your input is separated by newlines and do this
string index, name, id;
while (getline(in, index) && getline(in, name) && getline(in, id))
{
// do something with index, name and id
}
The advantage of this version is that it will work if you have spaces in your name. The disadvantage is that getline only works with strings. So you have to convert index from a string to an integer.
Just to get you on track,
int xyz;
in>>xyz;
This will read the integer at your first line, which you stored as "t". Now try to loop and read strings, line by line, and store them. Good luck.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
#include<fstream>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
ifstream infile;
ofstream outfile;
infile.open("oldfile.txt");
outfile.open("newfile.txt");
while(infile){
string str,nstr;
infile>>str;
char charr[10];
charr[0]='<';charr[1]='\0';
nstr=str.substr(0,str.find_first_of(charr));
outfile<<nstr<<' ';
}
}
this program uses substr(0, string.find_first-of(charcter array which is starting point to be substring))each word's unnecessary sub strings but it doesn't preserve the line number when writing to another file. can you fix it . it writes the file word by word sequencially. the code didn't preserve line by line,
string input doesn't care about line boundaries, it treats \n,\t,\v and probably others the same as space.
#include <sstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string line,word;
char foo[] = "<";
while ( getline(cin,line) ) {
string newline;
for ( istringstream words(line)
; words >> word ; ) {
newline+=word.substr(0,word.find_first_of(foo))+' ';
}
cout << newline << '\n';
}
}
Change
outfile<<nstr<<' ';
to
outfile<<nstr<<endl;
This will write line by line instead of sperating with a single whitespace character.