开发者

How could I open a file, read 9 seperate letters, and pass them all to another function? I struggle with arrays! (c++)

The text document is TTT.txt ...in it is

X
X
X
O
-
X
O
O
-

I need my program to read each and then be able to pass it to a dif开发者_运维技巧ferent part of it...HELP!


char stuff[9];
FILE* foo = fopen("myfile.txt", "r");

for (int i = 0; i < 9; i++) {
    stuff[i] = fgetc(foo);
    fgetc(foo); // Ignore the newline
} // Now your characters are in "stuff"

No error handling, hardcoded file name, and uses C functions. But it does what you want in a concise format.


Since this is homework, I'm not going to post actual code. but I'll give you a hint into the right direction.

What you need is file streams. You can read from a file using std::ifstream. It has several methods of reading. The one your looking for is probably operator >> reading into a char.

Read up on it, try to start off, and come back here when you get stuck, showing us what you have done so far and why you got stuck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜