If I have text-file wit 3 lines of text, how can I read that text into a char array and skip newlines?
Example:
myFile.txt:
abcdef
ghijklm
opqrst
myArray:
abcdefghijklmopqrst
If I have text-file wit 3 lines of text, how can I read that text into a char array and skip newlines?
Example:
myFile.txt:
abcdef
ghijklm
opqrst
myArray:
abcdefghijklmopqrst
Use the function fgetc to read all characters from the file and put all characters in the array one by one.
stream version:
char ch;
char array[256];
int i = 0;
ifstream fin ("yourfilename.yourfilenameextension");
//read in first cbar
fin >> ch;
//evaluate each char one by one
while(ch != EOF)
{
if(ch != '\n')
{
//if not EOF or newline the put char in first available array element
array[i++] = ch;
}
//get next char
fin >> ch;
}
//make array a strring by adding null terminating char
array[i] = '\0';
//dispaly array
cout << array << endl;