Help with file processing into array

Hi, I have some questions regarding reading files and storing in arrays.
1) Am I correct to say that this line of code which is
infile.getline(name[i],100); means that the system will read the string until the end of each line for about 100 columns?
2) Why when I change the value of 100 into a smaller number like 30, no output is shown? I have counted that there is only around 28 spaces from left to right of the textfile per row.
3) At line 4 which is --> char name[size][100]; , this indicates that it is a 2D array , but later on there is a code which is --> cout<< "\t" << name[x] <<endl; , indicates that the 2D array is already merged into one single array?


Textfile:
1
2
3
4
10034 Ali Mohammed Sabahi           
10112 Mona Ali Mansoor Sami         
10094 Jaad Depp                     
11002 Huah Wei Tan  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
 #include<iostream>
#include <fstream>
using namespace std;

int main()
{
	const int size=4;
	int ID[size];
	char name[size][100];
	
	int i=0;
	ifstream infile;
	infile.open("file2.txt");
	
	if (infile)
	{
		while(!infile.eof())
		{
			infile>>ID[i];
			infile.getline(name[i],30);
			i++;
		}
		
		cout <<"\n File read successfully \n";
		cout <<endl<<endl;
		infile.close();
		
		cout << " Data are as below :";
		cout<< "********************* \n";
		
		for(int x=0;x<size;x++)
		{
			cout << "\t" << ID[x] << endl;
			cout<< "\t" << name[x] <<endl;
			cout << "--------------------- \n"; 
		}
		
	}
	else
		cout << "failure to open file";

	return 0;
}
Last edited on
line 17 is trouble. you should instead say
while(getline), and not try to use the EOF flag this way.

getline(char *, number) is the C version for C-style strings (character arrays).
It will read up to number letters into the pointer.
getline(stream, string) is the C++ version, and reads from a stream (eg, cin or a file) into a c++ string object.

2) not sure what is going on there. I don't see any spaces except between words. Using stream operations with getline can cause trouble; you may need to clear the state (try infile.ignore). It could also be out of synch, if it reads too few in one loop then looks for an integer in the next but its still on the previous line and getting text into the int. If 100 works, leave it be? It may be easier to double getline, the first one can trigger off space (getline(char*, number, ' ') which overrides the default end of line break and uses space instead, then use atoi() to convert the text from the first getline back into an integer.

3) char array is a C style string and many c++ tools like cin work with those. A 2d array of characters is ALSO a one d array of C style strings. Here, think of it like that, as a one dimensional array of strings.

in general to debug this it is best to print everything as you go, in that while loop. Read from the file, print what you got, every time, so you can see what went wrong and where.
Last edited on
If I understand your questions:

A 2D array is an array of arrays. Your 2D name array is an array of C strings. Using operator[] to access the first element exposes the array contained inside that element. The contained C string.

Omitting the second operator[] accesses the entire contained C string.

The way C++ std::cout deals with C strings by default is to display the entire C string without the need to specify the elements.

This really shows why not learning about C++ containers first, teaching regular and C string arrays as if they are the only "proper" container(s) to use is really abusive to beginning C++ students. This is not C++ Programming 101, this is C Programming 512.

@sandbox007, We could show you C++ ways to do this assignment more effectively and efficiently, but your instructor clearly wants to torture you with IMO piss-poor and outmoded ways to do things.
Perhaps something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>

int main() {
	constexpr size_t size { 4 };
	constexpr size_t maxStr { 100 };

	std::ifstream infile("file2.txt");

	if (infile) {
		int ID[size] {};
		char name[size][maxStr] {};
		size_t noItms {};

		for (; noItms < size && infile >> ID[noItms] && infile >> std::ws && infile.getline(name[noItms], maxStr); ++noItms);

		std::cout << "\n File read successfully \n";

		std::cout << " Data are as below :";
		std::cout << "********************* \n";

		for (size_t x {}; x < noItms; ++x) {
			std::cout << "\t" << ID[x] << '\n';
			std::cout << "\t" << name[x] << '\n';
			std::cout << "--------------------- \n";
		}
	} else
		std::cout << "failure to open file";
}



File read successfully
Data are as below :*********************
       10034
       Ali Mohammed Sabahi
--------------------
       10112
       Mona Ali Mansoor Sami
--------------------
       10094
       Jaad Depp
--------------------
       11002
       Huah Wei Tan
--------------------


Note that >> leaves the terminating space in the file buffer which needs to be removed before .getline() as .getline() doesn't ignore leading spaces like >> does.

Re 1). ,getline(name[i], 100) will read a maximum of 100 chars of the contents of the line until either '\n' is found or end of data or 100 chars have been read. This number is usually the size of the char array into which the data is read.
Last edited on
Topic archived. No new replies allowed.