Bibek Bhattarai Bibek Bhattarai - 20 days ago 5
C++ Question

reading file into character array in C++

I have been writing code to read file into character array using C++ and tried to skip over white space characters but I am getting space and newline in my output array.

This is a function within class with label declared as character array.

//Read the label file
80 this->label = new char[vert_count+1];
81 std::ifstream file1;
82 file1.open(label_file);
83 if(file1 != NULL)
84 {
85 char temp; int i = 0;
86 std::cout << "Label file opened successfully" << std::endl;
87 if( i< vert_count)
88 {
89 file1.get(temp);
90 if(temp != ' ' && temp != '\n'&& temp != '\t')
91 {
92 label[i] = temp;
93 i++;
94 }
95 }
96 label[vert_count] = '\0';//putting null character to terminate
97 file1.close();
98 }
99 else
100 std::cout << "label file cannot be opened\n";
101 for (int i = 0; i< vert_count; i++)
102 std::cout <<label[i];


my input file is of format as shown below

a
b
c
d
e
f
1
2
H
M
O


The output I am getting is bunch of characters at start and then white space and new line only.

Answer

I think you meant (on line 87):

while (i < vert_count)