File to array is not working

Hello,

I'm trying to put the content of my file into an array and then cout it. But for some reason, the array couts only new lines but no text, or only hex-encoded tests.

My Code:
1
2
3
4
5
6
7
string array[50];
for (int i = 0; i < 5; i++) {
        file >> array[i];
        cout<< array[i] <<endl; //HEX encodes
        cout<< array <<endl; //Only empty new lines
}


file:
abc
def
ghi
jkl


The lines of the file should be placed into the arrays.

Besides that, which is faster?
For loops are while loops? Even if it matters only milliseconds?

Thanks for reading,
Niely
Last edited on
Question edited to explain it more clear.
closed account (SECMoG1T)
How is your data formatted inside your file, give us a preview.

cout<< array <<endl;///FYI this can only print the address of your "pointer" array
if possible post your entire code.
Last edited on
closed account (SECMoG1T)
Here is an example that works, i won't really understand why your code ain't working coz the snippet you gave above isn't enough to deduce the cause of errors, if you could provide the entire code that would be simpler to point out your errors however i can manage to give you some hints on what you can check on.

1. make sure the file is accessible to your program.
2. check if your file was successfully opened.
3. use your stream to control your loops it's better, your might not know before hand if
you've enough info in your file to fill your array .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    fstream in("file.txt");
    if(!in){cerr<<"file not found\n"; return(1);}///check stream

    string data,arr[5];
    size_t index=0;

    while(in>>data&&index<5)///your file must atleast contain 5 records
    {
        arr[index]=data;
        cout<<arr[index]<<endl;
        ++index;
    }
}


Am not sure if there is a speed aspect that will matter in your loops however different loops will suit perfectly different applications, for example:-
1. for loops are mostly used when the number of iterations are already know before
your program is executed.
2. while loops are mostly used when your are not sure of the number of iterations
required during program execution.

however i believe if your execute them on exactly equal number of iteration the differences in their execution time might be totally negligible.

hope that helps.
Last edited on
How do you mean data formatted inside file?

Whole code:
1
2
3
4
5
6
7
8
9
10
11
12
ifstream file(list);
string line1;
while(getline(file, line1)) {
amount++;
}

string array[50];
for (int i = 0; i < amount; i++) {
        file >> array[i];
        cout<< array[i] <<endl;
}
closed account (SECMoG1T)
Hadn't seen the update :<>) , it's ok though.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

string array[50];/// must exist in scope
ifstream file(list);///list must contain the file name.
 ///check if your file is open.

string line1;
int amount=0;
while(getline(file, line1)&&amount<50) {
///here is where you need to add data to your array
array[amount]=line1;/// av also added a check on your array size to avoid an
         ++amount       /// out of range exemption
}

for (int i = 0; i < amount; i++) {
        cout<< array[i] <<endl;
}


that should solve your bug.
Last edited on
Ain't working. All my variables ain't declared then.
Also, the while loop was intended to count how much lines the file has.
The size of the array would be the value of variable amount then.

Array 'array' is intended to hold extreme much values. It should be able to hold all the words who are in a wordlist.
closed account (SECMoG1T)
well now i get yah BTW still am convinced that my routine would be much safer to try, would you please post the errors your got, some correction on your method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ifstream file(list);
string line1;
while(getline(file, line1)) {
amount++;
}

///here your will need to close your stream or use seekg to reset it.
file.close();//in my example you dint need to close and reopen the file
file.open(list);///hope you get the idea now.
if(!file){/*your error code*/}

string array[50];
for (int i = 0; i < amount; i++) {///what if by mistake our amount was larger than 
        file >> array[i];              /// 50 and our array can only hold 50 indices , well 
        cout<< array[i] <<endl;/// an extra check will be more safer, i guess 
}


also to make your work even much easier i would suggest that you use library containers , you'll not be require to count the number of lines in your file, have a look here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::ifstream file(list);
std::tring line1;
std::vector<std::string> my_list.

while(getline(file, line1)) {
 my_list.push_back(line1);
}

for(const auto& wrd: my_list)
 {
    std::cout<<wrd<<std::endl;
 }
///i guess that would be even better.
Last edited on
Thanks a lot mate! That was exactly what I needed! :D
You were an awesome help.
Topic archived. No new replies allowed.