|
hi, i am writting a source code, to read a few lines of string from text file. for example, there are 5 lines, thus i will store each line (string) in a vector.
i compile my code by visual C++ (not .Net version), so ReadLine(), WriteLine() functions do not work. thus i write below code:
vector <string> ch;
vector <string> line;
string d;
do
{
d = infile.get(); // read each character of the line
ch.push_back(d); // store each character in vector ch
if(d == "\n")
break;
line.push_back(ch); // store each vector ch in vector line
}
while(d != "EOF");
this code doesn't work, i appreciate if anyone can help let me know the correct function or method i can use for this purpose...
|
|
|
Hi,
The problem is in the while loop.change it as follows
while(d != EOF);
EOF is defined in iostream.h file and the porblem in ur code was u was trying to use it as a string
Thanks
Raghuram
|
|
|
hi Raghuram, thank you very much for the reply...
but after i change to while(d != EOF) and compile, the error displayed like:
"binary '!=' : 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator or a con"
actually my code for store string in vector is correct or wrong?
i modified the code like below:
do
{
d = infile.get();
while(d)
{
ch.push_back(d);
if(d == "\n")
break;
}
line.push_back(ch); <== error
}
while(d != EOF);
but still have error after compile...
the error: "error C2664: 'push_back' : cannot convert parameter 1 from 'class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocat"
or do you know any method to store string (a sentence include the space bar) to vector?
thanks..
|
|
|
Hi,
Try this ...if u still face issue ..poost ur code fully...i will take a look
1)You have to change the vector declartions like this
vector <string> ch;
vector <vector<string> > line;
2)You can check whether u have reached endof file using the call
while(!infile.eof());
Raghuram
|
|
|
hi, i had tried but still error, i post the whole code here:
#include <iostream.h>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void copy_line(); //line copy function declaration
main()
{
copy_line();
return 0;
}
void copy_line()
{
vector <string> ch; //vector ch to store character
vector <vector<string>> line; //vector line to store each line
ifstream infile("/forum/cinput.txt", ios::in);
string d;
do
{
d = infile.get(); // read each character of the line
while(d)
{
ch.push_back(d); // store each character in vector ch
if(d == "\n")
break; //end loop if encounter next line
}
line.push_back(ch); // store each vector ch in vector line
}
while(!infile.eof()); //end when encounter the eof
infile.close();
// ofstream outfile("/forum/coutput.txt", ios::out);
// ch.WriteLine();
// outfile.close();
}
This code i write is to store each line in vector so that i can modify the line in the file by using these vectors.
Meanwhile, the function ch.WriteLine() is use to paste the line to another file, but it doesn't work because i use visual C++ (not .Net version). Is there any function can use to paste vector/line/string to a file?
|
|
|
Hi,
I have made some modification and it is compiling fine....check it
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void copy_line(); //line copy function declaration
main()
{
copy_line();
return 0;
}
void copy_line()
{
vector <string> ch; //vector ch to store character
vector <vector<string> > line; //vector line to store each line
ifstream infile("/forum/cinput.txt", ios::in);
string d;
do
{
d = infile.get(); // read each character of the line
//while(d)
{
ch.push_back(d); // store each character in vector ch
if(d == "\n")
break; //end loop if encounter next line
}
line.push_back(ch); // store each vector ch in vector line
}while(!infile.eof()); //end when encounter the eof
infile.close();
// ofstream outfile("/forum/coutput.txt", ios::out);
// ch.WriteLine();
// outfile.close();
}
I cant understand the usage of the condition while(d).
So i have commented it out.
Raghu
|
|
|
hi, i had tried and it can work, thanks.
but, maybe my code concept is wrong, because the character in the line was saved in the vector 'line'.
i wish to get the result as below:
from input.txt contents, for example:
Lot 31
Lot 32
Lot 33
then each line is saved in vector 'line' as:
line[0] = Lot 31
line[1] = Lot 32
line[3] = Lot 33
is my code method had went wrong?
|
|
|
Hi,
I think it is working fine...If u need some other stuff, post the required output.
Then i will check with the logic
Raghu
|
|
|
hi, when i run the above code with the input file in the example, the output of the vector are:
line[0] = L
line[1] = o
line[3] = t
how if i want to make it as:
line[0] = Lot 31
line[1] = Lot 32
line[3] = Lot 33
?
i wish to get output like this. thanks
|
|
|
HI,
Try this code.
This gets u what u expcted.
I have modified the code to some extent.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void copy_line(); //line copy function declaration
main()
{
copy_line();
return 0;
}
void copy_line()
{
vector <char> ch; //vector ch to store character
vector <vector<char> > line; //vector line to store each line
char read[256];
ifstream infile("/forum/ip_21.txt", ios::in);
string d;
char c;
do
{
while(infile.good())
{
c = infile.get(); // read each character of the line
if(c == '\n' || c == EOF)
{
break; //end loop if encounter next line
}
ch.push_back(c); // store each character in vector ch
}
line.push_back(ch); // store each vector ch in vector line
ch.clear();
}while(!infile.eof()); //end when encounter the eof
infile.close();
vector<char>::iterator cit;
vector <vector<char> >::iterator lit;
for(lit=line.begin();lit!=line.end();lit++)
{
for(cit=(*lit).begin();cit!=(*lit).end();cit++)
{
cout<<*cit;
}
cout<<endl;
}
// ofstream outfile("/forum/coutput.txt", ios::out);
// ch.WriteLine();
// outfile.close();
}
Raghu
|
|
|
hi Raghu, it's working fine. thanks a lot for your guide.
|
|
|
Beautiful blog with great informational content. Mostly certifications related and text file to vector related topics are really very good. 642-972 dumps, 642-974 dumps and 646-046 dumps are also good topics. Thanks for this great sharing.
|
|
|
I found a javascript somewhere that will MB5-292
parse through line at a time, stripping out all the html code and then I added code to parse the name/value of the line I want. It works fine at localhost on FireFox, but won't work a MB5-198t all when I move it up to the server. I keep getting an undefined value for the id. MB5-229 I think I'm going about this all wrong anyway, so I'd really appreciate some help. JavaScript is not my strongest language by any means. I'm kind of on a deadline here so sooner would be better than later if at all possible. MB5-199
|
|
|
|
|
With its high ground clearance, large wheels fitted with wide tires, its high waistline and broad shoulders, the Juke has a purely bottom car. This look is further accentuated by various typical SUV characteristics such as thresholds and black fender flares and rocker panels and rear before emerging in evoking protective underbody.With its high ground clearance, large wheels fitted with wide tires, its high waistline and broad shoulders, the Juke has a purely bottom car. This look is further accentuated by various typical SUV characteristics such as thresholds and black fender flares and rocker panels and rear before emerging in evoking protective underbody.With its high ground clearance, large wheels fitted with wide tires, its high waistline and broad shoulders, the Juke has a purely bottom car. This look is further accentuated by various typical SUV characteristics such as thresholds and black fender flares and rocker panels and rear before emerging in evoking protective underbody.
_____________________________________________________________ 642-631|| 650-177|| JK0-016|| 70-291|| 642-681|| 642-873
|
|
This 21 message thread spans 2 pages: [1] 2 > > |