nothing
12-21-2003, 11:18 AM
Let's say I have a .txt file that looks like this:
Programming
and
Web
Development
How do I assign the second line of this file to a string variable? Also, I would like to know how to control how many characters will be read into the string variable. Thanks so much :D
CompGeek01
12-21-2003, 12:51 PM
Having taken Java for the past few semesters, my C++ is scketchy..hehe..
#include <iostream>
#include <string>
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
string myWord;
fin.open( "text.txt", ios::in );
fin >> myWord;
fin >> myWord;
fin.close();
return 0;
}
This would basically get the second word and store it in a string, but I'm not sure what you mean inthe second part of your question. Do you mean..if the word is a certain length, read it in..else ignore it? Curious.
-B
nothing
12-21-2003, 03:49 PM
What I meant on the second question is let's say the second line of my .txt file looks like this:
Hello world
What if I want to assign only the word Hello to the string variable?
Dark_Raver
12-22-2003, 02:03 AM
you don't seem to be too clear on what the point/reson of this excercise is.
but what CompGeek01 showes absolutely will read in the second line of the txt file.
at this point you can do normal string operations on myWord. a simple search or a look in a reference book will show you all the possible operations on a string object to single out words and such.
DR
CompGeek01
12-23-2003, 09:30 PM
By nature I'm pretty sure the input operator in C++ has 2 natural delimiters (being ASCII codes 32 and 13, maybe more). So in my example...it would get 'world' as the final myWord. But it's been a while...try it.
-B
nothing
12-25-2003, 10:55 AM
#include <iostream>
using std::cout;
using std::endl;
using std::ios;
#include <string>
using std::string;
#include <fstream>
using std::ifstream;
#include <vector>
using std::vector;
int main()
{
ifstream infile("hours.txt", ios::in);
string input;
string foo;
vector<string> lines;
while (infile){
std::getline(infile, input);
lines.push_back(input);
}
foo = lines[1];
cout << foo << endl;
return 0;
}
A friend gave me this piece of code that will assign the second line of a .txt file to a string variable. When I compile it using MS Visual C++ 6.0, I get four warnings and I do not understand why.
Compiling...
Cpp8.cpp
D:\C++ stuff\Cpp8.cpp(32) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_trai
ts<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
D:\C++ stuff\Cpp8.cpp(32) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::basic_string<char,std::char_traits<cha
r>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(39) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
c:\program files\microsoft visual studio\vc98\include\vector(60) : warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
Cpp8.obj - 0 error(s), 4 warning(s)
Can someone tell me why I am getting these warnings and how to get rid of them? Thank you very much!
fishybawb
12-26-2003, 10:07 AM
Some of the vector identifiers are too big to be stored in the debug information - don't worry about it. If you go to "Build->Set Active Configuration->Win32 Release", the warnings go away.
:t