Array and txt file

Hi I have a 4 by 4 comma separated txt file named val.txt that looks like

1,2,3,4
2,4,6,4
1,2,7,8
9,3,1,2

for example. I want to read it into an 4 by 4 array and I am struggling to find any starting point.

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

int main()
{
ifstream infile;
infile.open("val.txt");

if (!infile)
{
cout << "Fail" << endl;
}
else
{
stringstream ss;
ss << infile;
cout << ss.str() << endl;
}
return 0;
}


this was just to see if it would take the values but it didnt work so I am lost.
assuming the file is exactly what you said, no error checking etc:
try this:

1
2
3
4
5
int arr[4][4];
int index{};
char c;
while(infile >> arr[index++])  //while not end of file read into arr.  exploiting 2d array memory layout
infile>>c; //get rid of the commas 


and print your 4x4 array to see if it worked.
Hello Petrus1234,

While I look into your code.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Andy
Possibly:

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

int main()
{
	constexpr size_t arrsz {4};
	std::ifstream infile("val.txt");

	if (!infile)
		return (std::cout << "Fail\n"), 1;

	int array[arrsz][arrsz] {};

	for (size_t r = 0; r < arrsz; ++r)
		for (size_t c = 0; c < arrsz; ++c, infile.ignore())
			infile >> array[r][c];

	for (size_t r = 0; r < arrsz; ++r, std::cout << '\n')
		for (size_t c = 0; c < arrsz; ++c)
			std::cout << (c != 0 ? " " : "") << array[r][c];
}



1 2 3 4
2 4 6 4
1 2 7 8
9 3 1 2

Last edited on
Thank you Handy Andy for the tip as I am new to this forum this helps alot.
Thank you jonnin and seeplus I will try both!
seeplus what is the function of your line 6 I have never seen that before ?
constexpr just means that the initialisation of a const is done at compile time if possible - not at runtime.
A constexpr variable must be initialised at compile time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   const int N = 4;
   int a[N][N];

   ifstream in( "val.txt" );
   char comma;
   for ( int i = 0; i < N; i++ )
   {
      in >> a[i][0];
      for ( int j = 1; j < N; j++ ) in >> comma >> a[i][j];
   }

   cout << "Check:\n";
   for ( int i = 0; i < N; i++ )
   {
      for ( int j = 0; j < N; j++ ) cout << a[i][j] << '\t';
      cout << '\n';
   }
}


Check:
1	2	3	4	
2	4	6	4	
1	2	7	8	
9	3	1	2	

Last edited on
Topic archived. No new replies allowed.