Hey everyone, I have a simple simple problem that is literally driving me insane.

Basically, I have a database of numbers seperated my spaces. I just took a segment of my code because I am soooo completely lost with the output!!

If the database is:

1 2 3 9 7 6 4 5 6 7

I don't understand why my output is 1 for every single line??? How is the sscanf statement not taking the next string??

Someone please help I am desperate!!

#include <stdio.h>

main()
{
	
	char buffer[20000];
	char pid_temp[501], arrival_time_temp[501], CPU_burst_temp[501], IO_burst_temp[501];	
	FILE *fp;
	
	fp = fopen("CPULoad.dat", "r");
	fgets(buffer, 20000, fp);	
	
	fp = fopen("CPULoad.dat", "r");
	fgets(buffer, 20000, fp);
	
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	
	close(fp);
}

Edited 6 Years Ago by theonlywalks: mistake in title

Hey everyone, I have a simple simple problem that is literally driving me insane.

Basically, I have a database of numbers seperated my spaces. I just took a segment of my code because I am soooo completely lost with the output!!

If the database is:

1 2 3 9 7 6 4 5 6 7

I don't understand why my output is 1 for every single line??? How is the sscanf statement not taking the next string??

Someone please help I am desperate!!

You have a data base or a data file?

It's obvious that your code doesn't run.

fp = fopen("CPULoad.dat", "r");
	fgets(buffer, 20000, fp);	
	
	fp = fopen("CPULoad.dat", "r");
	fgets(buffer, 20000, fp);

You cannot open the same file twice, so you never get to any output at all.

Please either describe the real problem or post the real code.

Hi, I'm sorry, I was copy and pasting my code and I pasted part of it twice for some reason.

Basically, I have a text file that is like this:

1 2 4 8 11 66 77 11

So I use fgets to put the first line into a buffer, but when I try to read from the buffer, with sscanf, it only reads the first number, over and over again.

How do you read subsequent numbers, or strings? My output looks like:

1
1
1
1
1
1

instead of:
1
2
4
8
11
...

#include <stdio.h>

main()
{
	
	char buffer[20000];
	char pid_temp[501], arrival_time_temp[501], CPU_burst_temp[501], IO_burst_temp[501];	
	FILE *fp;
	
	fp = fopen("data", "r");
	fgets(buffer, 20000, fp);	
	
	
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	
	close(fp);
}

Here's what I see.

sscanf(buffer, "%s", CPU_burst_temp);  // what is the value of buffer here?
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);  // what is the value of buffer here?
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);  // what is the value of buffer here?
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);  // etc...
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);
	sscanf(buffer, "%s", CPU_burst_temp);
	printf("%s\n", CPU_burst_temp);

What is changing buffer?

Edited 6 Years Ago by WaltP: n/a

hmm, well the buffer doesn't change, and the value of CPU_burst_temp remains 1 at all the statements.

But I thought that sscanf would read ignore the whitespace, and read the string until either the char array filled up, or it found an EOF or whitespace. And then the next time you invoke sscanf again, it would continue scanning where it left off?

It ignores preceding whitespace. But If you don't remove the '1', buffer never changes so you keep reading it.

It ignores preceding whitespace. But If you don't remove the '1', buffer never changes so you keep reading it.

ahh, i see, would you mind telling me how to remove the one, or advance the pointer position because I am not sure how to.

You're using sscanf as if it were fscanf. You can make the two behave similarly, but sscanf will need different directives and variables.

You could consider strtok instead of sscanf.

Parsing a String into Tokens Using sscanf

Many times strtok is recommended for parsing a string; I don't care for strtok . Why?

  • It modifies the incoming string, so it cannot be used with string literals or other constant strings.
  • The identity of the delimiting character is lost.
  • It uses a static buffer while parsing, so it's not reentrant.
  • It does not correctly handle "empty" fields -- that is, where two delimiters are back-to-back and meant to denote the lack of information in that field.

This snippet shows a way to use sscanf to parse a string into fields delimited by a character ...

The empty fields may be about the only thing that comes closest.

But anyways, the above linked snippet shows how to use sscanf to parse multiple elements from the same line. The earlier posted attempt seemed to be using sscanf as if it were fscanf (which could also be used if you so choose) -- as illustrated by WaltP (scanning the same text over and over and expecting different results).

This article has been dead for over six months. Start a new discussion instead.