Hello there, I am having trouble copying text into a string, what am i doing wrong?

#include <stdio.h>

int main() {
	char instructions[80];
	
	FILE *f;
	f=fopen("a3.txt","r");
	
	while (fscanf(f,"%s",instructions != EOF){
		fscanf(f,"%s",instructions);
	}
	
	printf("%s",instructions);

	
	
}

the contents of a3.txt are
B404
239A
2412
5345
350C
C000.

Recommended Answers

All 3 Replies

Hello there, I am having trouble copying text into a string, what am i doing wrong?

#include <stdio.h>

int main() {
	char instructions[80];
	
	FILE *f;
	f=fopen("a3.txt","r");
	
	while (fscanf(f,"%s",instructions != EOF){
		fscanf(f,"%s",instructions);
	}
	
	printf("%s",instructions);

	
	
}

the contents of a3.txt are
B404
239A
2412
5345
350C
C000.

There are two things wrong with your code. In the 'while (fscanf(f,"%s",instructions != EOF)' line you have a missing "close paren". It should be 'while (fscanf(f,"%s",instructions) != EOF)'. The next problem is that you are reading your file twice. Once in the while statement and once in the body of the while statement. I would do the following

while (fscanf(f,"%s",instructions) != EOF){
printf("%s",instructions);
}

commented: good explanation +13

How It can be If we want to get from text to 2D string.

Regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.