-
May 29th, 2006, 02:41 AM
#1
converting string to int from files
hi
I'd like to convert a string which contains many numbers into int and then store them in an array.
the file.txt looks like this:
54 42 432 121
0 23 435 32
43 343 654 45
12 5 65 23
the procedure that I'm doing is this:
ifstream f81;
ofstream f81r;
f81.open("f81.txt");
f81r.open("f81r.txt");
int i;
string line;
while(!f81.eof())
{
getline(f81,line);
for(int k=0;k<32;k++) //my matrix size is 32x32
{
f[i][k]=?????? // help needed here.
}
i++;
}
thanks in advance.
-
May 29th, 2006, 03:34 AM
#2
Re: converting string to int from files
Have a look at stringstream.
-
May 29th, 2006, 06:17 AM
#3
Re: converting string to int from files
-
May 30th, 2006, 01:23 AM
#4
Re: converting string to int from files
thanks for the reply...
I found another method to do it, but I have a little problem.
I tryed to use a getline function (from #include<string> ) but it give me an error in both C++ and Visual C++
also if I tryed to use string variable it doesn't work.
this is the code:
#include<string> // I tried string.h but also didn't work
.
.
CString line; // I tryed to type it string and give me the same error *
while(!f81.eof()) // I'm getting the line from a file
{
getline(f81,line); // another error **
*error C2065: 'CString' : undeclared identifier or 'string'
**error C2065: 'getline' : undeclared identifier
i tryed to read the faqs but only find the #include <afx.h> and the cstring is working, but still the getline function doesn't work.
any help is appreicated.
-
May 30th, 2006, 01:25 AM
#5
Re: converting string to int from files
Use std::string, not CString. Use std::getline, not getline (unless you have a using std::getline statement).
-
May 30th, 2006, 01:38 AM
#6
Re: converting string to int from files
I never used the std:: () statement...and in the book i have it's only string, getline....I'm a bit confused.
-
May 30th, 2006, 01:50 AM
#7
Re: converting string to int from files
Your book might be outdated. You should read up on namespaces. Anyway, since your book only mentions string, you should use string (i.e. std::string), not CString.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|