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"?

Recommended Answers

All 4 Replies

Is it a requitement to use c-style char arrays instead of c++ strings? Using strings simplifies your code tremendously. Also unless you know ahead of time how many records you'll be reading it makes more sense usually to use a dynamic collection like vector. That being said, with the code you've shown, I don't get the same results, 'potatoes' only gets written once.

Here's the same basic code using strings & vectors instead:

using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

int main()
{
    vector<string> food;
    string buffer;
    ifstream iFile;
    ofstream oFile("food.txt");
    oFile << "Candy\n" << "turkey\n" << "steak\n" << "potatoes\n";
    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");
    while (iFile >> buffer)
    {       
            food.emplace_back(buffer);
    }
    iFile.close();
    ofstream oFood("oFood.txt");
    for (string s : food)
    {
        cout << s << '\n';
        oFood << s << '\n';
    }
    oFood.close();
    return 0;
}

Ah, thanks. Yeah, I'm well aware of vectors and Strings, I have an assignment that only allows for cstrings. So, I'm using the code above to practice and get more comfortable instead of learning as I write the more complex assignment. I like to break problems down into smaller components and deal with 'em piecemeal.

I appreciate the code and will definately look at/play with it!

Since c-strings is a requirement here's some more code to look at. I tightened a few things up for you. One notable thing, instead of constantly making the next char '\0', just initialize to '\0'. Also you can have the output for cout and oFood in the same loop:

using namespace std;
#include <iostream>
#include <fstream>
int main()
{
    char food[10][15] = { '\0' };
    char buffer[15] = { '\0' };
    ifstream iFile;
    ofstream oFile("food.txt");
    oFile << "Candy\n" << "turkey\n" << "steak\n" << "potatoes\n";
    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;
    while (iFile >> buffer)
    {
        for (int i = 0; buffer[i] != '\0'; i++)
            food[r][i] = buffer[i];
        r++;
    }
    ofstream oFood("oFood.txt");
    int i = 0;
    while (food[i][0] != '\0')
    {
        cout << food[i] << '\n';
        oFood << food[i++] << '\n';
    }
    oFood.close();
    return 0;
}

Thanks so much! I don't know why I didn't think to initialize it to '\0' what a great idea. Thanks for all your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.