I've hit a wall. I'm trying to open a .txt file and then copy it's contents into an array of character arrays. Can someone point in me in the right direction. I've been at this for about 2 days now and haven't made any head way. Below is a sample of what I'm trying to do:
using namespace std;
#include <iostream>
#include <fstream>
int main()
{
char food[10][15] = {' '};
char buffer[15];
ifstream iFile("food.txt");
iFile.close();
ofstream oFile("food.txt");
oFile << "Candy" << endl << "turkey" << endl << "steak" << endl << "potatoes" << endl;
oFile.close();
// Up to here, everything's hunky dory, when I try to read into the char array
// is where I'm just plain stuck.
iFile.open("food.txt");
int r = 0;
int c = 0;
while(!iFile.eof())
{
iFile >> buffer;
c = 0;
while(buffer[c] != '\0')
{
food[r][c] = buffer[c];
food[r][c + 1] = '\0';
c++;
}
r++;
}
for(int i = 0; i < 6; i++)
{
cout << food[i] << endl;
}
ofstream oFood("oFood.txt");
r = 0;
c = 0;
while(food[r][c] != '\0')
{
oFood << food[r];
r++;
oFood << endl;
}
oFood.close();
return 0;
}
output:
Candy
turkey
steak
potatoes
potatoes
Why am I getting this extra "potatoes"?