File IO read lines.
Jun 3, 2014 at 9:22pm UTC
I am accessing this file and reading 4 lines of mixed integers and strings.
When I output the buffers it seems my loop only reads the first line in the file.
How do I get the next line of file data?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
fstream reMixedFile;
std::string siftFile(string charPoint, int word);
const int VAL = 21;
int numbers[VAL];
string words[VAL];
std::string Buffer;
std::string buffer;
int newSpace = 0;
int w = 0;
int n = 0;
int space = 0;
int main()
{
reMixedFile.open("mixed.bin" , ios::in);
while (getline(reMixedFile, Buffer))
{
newSpace = 0;
while (newSpace < Buffer.length())
if (Buffer[newSpace] == ' ' ) newSpace++;
else
{
buffer = siftFile(Buffer, newSpace);
newSpace = Buffer.find(' ' , newSpace);
if (isalpha(buffer[0]))
{
words[w]=buffer;
w++;
}
else if (isdigit(buffer[0]))
{
numbers[n] = atoi(buffer.c_str());
n++;
}
}
reMixedFile.close();
for (int i = 0; i < w; i++)
cout << words[i] << ' ' ;
cout << "\n\n" ;
for (int c = 0; c < n; c++)
cout << numbers[c] << ' ' ;
}
return 0;
}
string siftFile(string charPoint, int word)
{
int lastChar = charPoint.find(' ' , word) - word;
return charPoint.substr(word, lastChar);
}
Jun 3, 2014 at 9:25pm UTC
Err...you're closing the file inside the loop that's trying to read all of the lines.
Try moving the closing curly brace on line 52 to between lines 44 and 45.
Jun 4, 2014 at 1:09am UTC
Thanks im glad you caught that. I was up all night working on this and in the morning I just had to focus, it wasn't coming to me.
Topic archived. No new replies allowed.