A
B
C
D
And I want to read the characters one by one to four variables CHAR using a loop.
But I do not know how to do it
Thanks in advance.
Posted 19 July 2012 - 08:30 AM
Posted 19 July 2012 - 08:40 AM
#include <fstream>
#include <iostream>
int main(int argc, char **argv)
{
if (argc != 2)
return 1;
std::ifstream file(argv[1]);
char ch = 0;
while (file >> ch)
{
std::cout << "read char: " << ch << '\n';
}
return 0;
}
Edited by edd, 19 July 2012 - 04:07 PM.
Posted 19 July 2012 - 11:11 PM
char characters[100];
char loadline[100];
fstream input;
input.open("fontprops.txt");
input >> loadline;
for(int i = 0;i < 100;i++)
{
characters[i].number = i;
characters[i].character = loadtext[i];
}
Edited by iwoplaza, 19 July 2012 - 11:12 PM.
Posted 20 July 2012 - 07:31 AM
Instead of loading the characters one by one, I loaded a line of characters to an array of char.
.
Example:char loadline[100]; fstream input; input.open("fontprops.txt"); input >> loadline; [...]
#include <string>
// ...
std::string loadline;
std::ifstream input("fontprops.txt");
input >> loadline;
if (loadline.size() != 100)
{
std::cerr << "expected 100 characters, got " << loadline.size() << std::endl;
return false;
}
for (int i = 0; i < 100; ++i)
{
// use loadline[i] in the same way to get the char at index i
}
Edited by edd, 20 July 2012 - 07:35 AM.
Posted 23 July 2012 - 11:09 AM
Thanks to all, but I solved it for myself
.
I changed the contents of a text file from:
a
b
c
d
...
(one hundred characters)
to:
abcdefghijk ... (one hundred characters)
Instead of loading the characters one by one, I loaded a line of characters to an array of char.
.
Example:char characters[100]; char loadline[100]; fstream input; input.open("fontprops.txt"); input >> loadline; for(int i = 0;i < 100;i++) { characters[i].number = i; characters[i].character = loadtext[i]; }