.txt into a char

Pages: 12
Sep 19, 2015 at 10:52am
So I'm really new and I just want to get "all.txt" into the char temp.
Sep 19, 2015 at 11:30am
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?
Sep 19, 2015 at 12:18pm
As I said I'm new, so uh it's just defined as "char temp"
Sep 19, 2015 at 1:02pm
a char can only hold a single character. So unless your file ontains only one character, you would not be able to do so.
Sep 19, 2015 at 3:29pm
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 Sep 19, 2015 at 3:47pm
Sep 19, 2015 at 4:37pm
is it possible to read the text file line by line?
http://coliru.stacked-crooked.com/a/0fe96b99b342d244

Sep 19, 2015 at 4:57pm
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 Sep 19, 2015 at 4:58pm
Sep 19, 2015 at 5:15pm
You can also process file char by char: http://coliru.stacked-crooked.com/a/96f4416c19f7665e
Sep 19, 2015 at 5:25pm
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 Sep 19, 2015 at 5:36pm
Sep 19, 2015 at 5:39pm
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
Sep 19, 2015 at 5:49pm
Can you tell me more about 2?
Sep 19, 2015 at 6:20pm
Sep 19, 2015 at 6:32pm
I understand the principle, but where do I insert that code?
Sep 19, 2015 at 6:38pm
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.
Sep 19, 2015 at 6:39pm
I see, but there is a slight issue, I'm running my program from visual studio
Sep 19, 2015 at 6:42pm
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 Sep 19, 2015 at 6:43pm
Sep 19, 2015 at 6:42pm
Sep 19, 2015 at 7:04pm
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
Sep 19, 2015 at 7:09pm
It is possible by either executing batch file through WinAPI or by requesting command line interpreter to do this using system() call
Sep 19, 2015 at 7:10pm
Okay perfect, will the program wait for it to finish? And also is it possible to target an upper folder?
Last edited on Sep 19, 2015 at 7:10pm
Pages: 12