codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
This 21 message thread spans 2 pages: [1]  2  > >  
  store string from text file to vector  peace at 09:19 on Thursday, May 24, 2007
 

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...

  Re: store string from text file to vector  gpraghuram at 10:30 on Wednesday, May 30, 2007
 

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

  Re: store string from text file to vector  peace at 00:51 on Thursday, May 31, 2007
 

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..



  Re: store string from text file to vector  gpraghuram at 04:31 on Thursday, May 31, 2007
 

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

  Re: store string from text file to vector  peace at 05:49 on Thursday, May 31, 2007
 

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?

  Re: store string from text file to vector  gpraghuram at 06:01 on Thursday, May 31, 2007
 

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

  Re: store string from text file to vector  peace at 07:48 on Thursday, May 31, 2007
 

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?

  Re: store string from text file to vector  gpraghuram at 08:25 on Thursday, May 31, 2007
 

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

  Re: store string from text file to vector  peace at 09:13 on Thursday, May 31, 2007
 

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

  Re: store string from text file to vector  gpraghuram at 04:59 on Friday, June 01, 2007
 

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

  Re: store string from text file to vector  peace at 03:16 on Sunday, June 03, 2007
 

hi Raghu, it's working fine. thanks a lot for your guide.

  Re: store string from text file to vector  amery buck at 09:59 on Saturday, March 20, 2010
 

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.

  Re: store string from text file to vector  stevemartyn at 05:51 on Monday, October 25, 2010
 

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



  Re: store string from text file to vector  dabora at 09:54 on Tuesday, January 04, 2011
 

I have had arthritis in my lower spine for about 15 years,the last month or so I have been getting increasing pain in my hip / pelvis really painful when sitting I have to lay straight in bed on my side, but can only lay in one position for about 20 mins,
I don't know it this pain is due to the arthritis in mt spine. has anyone had anything similar ??
testking 1z0-007|| testking 156-215|| testking 156-910.70||testking 642-437|| testking 642-617|| testking 412-79


  Re: store string from text file to vector  dabora at 06:32 on Saturday, January 15, 2011
 

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  > >  







CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: how to create forum using asp.net with c# language?
•  Re: Change link style (color) based on current page (url)
•  Re: how can i make link
•  Re: drop down tool tip
•  Re: Auto Reply
•  Re: Disable reply on mail
•  Re: image in asp.net control from sql server as image datatype
•  Re: Doubles .toString Error: double cannot be dereferenced.
•  louis vuitton handbags


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2011