.txt into a char

Pages: 12
So I'm really new and I just want to get "all.txt" into the char temp.
Are you trying to get the data in "all.txt" inside a char template or are you trying to store data in your "all.txt" inside a char variable?
As I said I'm new, so uh it's just defined as "char temp"
a char can only hold a single character. So unless your file ontains only one character, you would not be able to do so.
hmm, perhaps I need to see more into the code, I'm trying to replace a
scanf("%c", &temp);
so basically skip the process of inputting that
and that is running on a loop, so is it possible to read the text file line by line? The char temp is moved to a more permanent storage in the loop.
Last edited on
is it possible to read the text file line by line?
http://coliru.stacked-crooked.com/a/0fe96b99b342d244

I mean 1 line into the char temp per loop, but that seems otherwise perfect
And what if the file was only 1 character, would that be possible to read into the char temp?
Last edited on
You can also process file char by char: http://coliru.stacked-crooked.com/a/96f4416c19f7665e
This is what I have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	for (int y = 0; y < 4; y++)
	{
		for (int x = 0; x < 4; x++)
		{
			char temp;

			scanf("%c", &temp); //I want to automate this to read from a text file

				puzzle[x][y].val[0]=temp;
				puzzle[x][y].length=1;
				puzzle[x][y].type=NORMAL;

		}
	}

Yeah, and I'm not sure at all how to do this
There is also alot of other code, but this is inside int main()

And also, this code isn't mine, I'm just trying to automate it
Last edited on
1) You can replace each call to scanf with fscanf which will read from supplied file

2) You can just redirect input stream, forcing program to read from file.
On windows: program.exe < myfile.txt
Can you tell me more about 2?
I understand the principle, but where do I insert that code?
You do not insert it anywhere. You run your program like that.

Normal run: type program.exe in command line
Redirected input: type program.exe < input.txt in command line

This is exact purpose of redirecton: to allow program to take input from another source without changing its code.
I see, but there is a slight issue, I'm running my program from visual studio
I tried the other way, but it doesn't seem to work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	FILE * pFile;
	pFile = fopen("all.txt", "r");

	char *tmp="ast";
	for (int y = 0; y < 4; y++)
	{
		for (int x = 0; x < 4; x++)
		{
			char temp;
			fscanf(pFile, "%c", &temp);
				puzzle[x][y].val[0]=temp;
				puzzle[x][y].length=1;
				puzzle[x][y].type=NORMAL;
		}
	}

	

Can you spot what's wrong? Do I need to make it open a different file for each character?
Last edited on
Okay, I got the redirected input to work, is it possible to run a batch file through a c++ program, and sorry for asking so many possibly silly questions
It is possible by either executing batch file through WinAPI or by requesting command line interpreter to do this using system() call
Okay perfect, will the program wait for it to finish? And also is it possible to target an upper folder?
Last edited on
Pages: 12