-
please help!
Hi,
I just try to write a program in C++. I knew it is very easy. but I can't not figure out whai's wrong?
I want to read a very long string in a .txt file, and then split this string into many substrings (the length of each substring is 50).
My code like this:
#include <fstream>
#include<string>
#include<cctype>
#include <iomanip>
using namespace std;
int main()
{
string text;
string s[100000];
int i,j,n,L,ll;
L=50;
const char* filename="aa.txt";
ifstream inFile(filename);
if (!inFile)
{
cout<<endl<<"Failed to open file"<<filename;
return 1;
}
inFile>>text;
ll=text.length();
n=ll/L;
i=0;
while (i<n)
{
j=i*L+1;
s[i]=text.substr(j,L);
i++;
}
}
-
inFile>>text;
That will only read in one word. The operator >> stops reading input when it encounters a whitespace, so n=ll/L may equal zero. What does your input file look like?
j=i*L+1;
If i=0, j=1 and you start the substring at position 1, but the string index starts at 0. Is that what you want? If text.length() is 5, the indexing goes from 0-4.
Last edited by 7stud; 05-03-2003 at 09:08 PM.
-
Pursuing knowledge
Popular pages
Recent additions