spy-killer
spy-killer

Reputation: 405

Reading a 2D char array from file in C++

First off my knowledge of c++ is very limited this is my first class so this may look dumb but bear with me, i would ask that you please keep it somewhat simple or explain very well thanks in advance.

Ok the goal is i want to create this file with the rules inside so that i can read it in in a separate function. However when i run this i get no errors but it comes out firstly with no spaces at all and i get some random ascii at the end.

here is the results of running the code Welcometothetypinggame1.Typetheentiresentenceinoneline2.IstimedandwillaffectthescoreØ-™wîú{ Dx&a¼ Ë' ©ÉaDx&a®pa

#include <cstdlib>
#include <fstream>
#include <iostream>

int main(int argc, char** argv) {
//set the sentences i want the file to print
char o[3][40]={"Welcome to the typing game             ",
            "1. Type the entire sentence in one line",
            "2. Is timed and will affect the score  "};

//creating the file to read in
ofstream out;
out.open("rules.txt");
for(int i=0;i<3;i++){
    for(int x=0; x<39; x++){
        out<<o[i][x];
    }
    out<<"\n";
}
out.close();


//creating a new array to read the data that was stored above
char a[3][40];
ifstream in;
in.open("rules.txt");
for(int i=0;i<3;i++){
    for(int x=0; x<40; x++){
        in>>a[i][x];
    }
}
in.close();

//and just printing out the array to see the results
for(int i=0;i<3;i++){
    for(int x=0; x<40; x++){
        cout<<a[i][x];
    }
}
return 0;
}

Upvotes: 1

Views: 5335

Answers (5)

tylerfb11
tylerfb11

Reputation: 43

If i remember correctly, the lack of /n at the end of each line would prevent the 'wordwrap' and it would all appear on one line.

Upvotes: 0

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22214

The extraction operator >> will ignore white spaces by default. You can either read unformated data as suggested in the other answers or you can change the default behavior like this :

in.open("rules.txt");
in >> noskipws;
for(int i=0;i<3;i++){
    for(int x=0; x<40; x++){
        in>>a[i][x];
    }
}

Upvotes: 1

Federico
Federico

Reputation: 1090

Part 1

it comes out firstly with no spaces

the problem is here

for(int x=0; x<39; x++){
   out<<o[i][x];
}

You write only the data, and you do not format your output.

You have two possible solutions:

  1. use <iomanip> and use setw and setfill
  2. add a whitespace after (or before) every entry.

Example for the second option:

for(int x=0; x<39; x++) {
    out << o[i][x] <<" ";
}

Obviously this will put spaces also after the last entry (or before the first). Additional code is required if you do not want this.

Part 2

i get some random ascii at the end

The problem (as stated by others) lies here:

for(int i=0;i<3;i++){
    for(int x=0; x<39; x++)

You arbitrarily limit/increase your output disregarding the actual size of the variable you have to write/read.

For writing (it is the same for screen or file) you should do something like (please note that this is a solution if o is a vector of vectors, not a char array. I prefer not to use arrays.)

for(int i=0;i<o.size();i++){
    for(int x=0; x<o[0].size(); x++)

For reading, instead, you need to check if you are changing line and/or if you reached the end of the file.

Upvotes: 0

Martol1ni
Martol1ni

Reputation: 4702

Do not use >> in this particular example. It reads formatted data from your file. Either use

in.get(a[i][x]);

Or even better, drop your char arrays and declare them as strings and put them in a dynamic container, let's say vector.

ifstream in("rules.txt");
vector<string> myLines;
while (!in.eof()) {
    string temp;
    getline(in,temp);
    myLines.push_back(temp);
}

Now you have all your rules in separate strings stored in a vector. It is also easy to write them to a file. When you are iterating with your for loop from 0 to 40, and the string is not 40 characters long, you get your "random ascii".

Upvotes: 2

Donotalo
Donotalo

Reputation: 13025

The problem lies here:

in>>a[i][x];

The extraction operator >> extracts formatted data. Formatted data means that blocks of characters separated by whitespace (like space). During extraction, all whitespace characters are ignored. So you can't read whitespace character using >> operator.

What you need to do is to read as unformated data:

in.get(a[i][x]);

Read more here and here.

Upvotes: 5

Related Questions