I need c++ code to copy the contain of a file to an array. the file contains four row and the length of the file is 104. how can i copy the contains of the file to an two dimensional array.
Printable View
I need c++ code to copy the contain of a file to an array. the file contains four row and the length of the file is 104. how can i copy the contains of the file to an two dimensional array.
fstream
vector
Hi, I do not know the rows and columns of the file. So, How can I copy the contains of a file to an two dimensional array. please let me know.
Just a hunch, but are you in the same class as andre.heller? He posted a suspiciously similar question.
If your file looks like what Feoggou posted, then you can use a CSV loader.
The data in the file stores like that:
ABSDEWGRTERTVSDTERYGETTR7U-TERTRET-RET-ERTER-T
SADGRYREYGRTYREGTRET-TREWRTERTRETRETTR-ERTER-T
SDFSDFSDFSDFSSFSFSFS-DSFDSFDSFDSFSDFSD-SDFDA-S
DFSDFDSFSDFDFSDFDSFF-SDFSDFDFSDFDSFDSF-SDFSDF
I need to copy this contains from the file to a two dimensional array.
you can use something like this:
this will read maximum 100 characters, but will not take the '-' characters.Code:fstream f;
char s[100];
//after opening it
fs.get(s, 100, '-');
that is, the first call will read
ABSDEWGRTERTVSDTERYGETTR7U
and put the read text into the string s.
by the way, two dimensional array means that each row must have the same number of elements (if '-' is the delimiter, I see only 3 elements in the last line, and 5 elements in the first!)
how do you expect the resulting two-dimensional array to look like?
I want to copy the whole contents of the file to a two dimensional array including the '-' character. the contains of the array should be each line of the file in one row.
that's a one dimensional array (where each element of the array is a line in the text file).
Code:
#include <vector>
#include <fstream>
using namespace std;
....
int main()
{
...
fstream f;
char s[100];
vector<string> v;
...
//after opening it
do
{
string str;
fs.getline(s, 100);
str = s;
v.push_back(str);
} while (!f.eof());
//now you have the one dimensional array filled. do what you want with it here:
...
return 0;
}
I want to fill that in two dimensional array.
again, and how do you expect that two-dimensional array to look like?
two dimensional arrays look like this:
(x11 x12 x13 x14)
(x21 x22 x23 x24)
(x31 x32 x33 x34)
where each x is an element of the two-dimensional array. And, as you can see, there is the same number of elements for each row (in this case, 4) and the same number of elements for each column (in this case, 3).
anyway, I don't know if your data can be put in that which is called "two dimensional array" because your data doesn't have the same number of elements in each row (as, from the data you have written, I understand the strings in capital letters to be the elements of the 'array').
[ removed duplicate threads ]
Do not start multiple threads on the same problem!