curious to know y u need to use sscanf instead of fscanf? if you are reading a file, you should use fscanf.
Main Topics
Browse All TopicsHi All,
I am using sscanf function to read a text file with syntax like this
1 Record One With Space
2 RecordTwo
3 Record Three with space
How can I use the sscanf function to acheive this?
IntegerVar[0]=1, FieldContent[0] = "Record One With Space"
IntegerVar[1]=2, FieldContent[1] = "RecordTwo"
IntegerVar[2]=3, FieldContent[2] = "Record Three with space"
Please advise. Thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here's one way of many possible ways, using streams.
#include <fstream>
#include <iostream>
#include <sstream>
int main() {
std::ifstream file("Data.txt");
std::string aLine;
while( getline(file, aLine, '\n')) {
std::istringstream iss(aLine); // istringstream provides convenient extraction
int value;
iss >> value; // whitespace-terminated value
std::string theRest, section;
while (!iss.eof()) { iss >> section; theRest += " "; theRest += section; }
std::cout << value << ", " << theRest << std::endl;
}
}
>> Other than sscanf function, what other functions can I use to achieve what I want listed at first?
sscanf is fine, provided you use something like :
char *my_str = "1 Record One With Space";
int the_number = 0;
char the_str[256] = { 0 };
sscanf(my_str, "%d %[^\r\n]", &the_number, the_str);
But the C++ way would be to use streams like josgood showed in his last post.
Business Accounts
Answer for Membership
by: josgoodPosted on 2007-08-30 at 03:56:07ID: 19798847
This answer is crude in its handling of string sizes (fixed 256), but serves to illustrate one way.
rm3,term4) ; 4);
FieldConte nt[0][0]); FieldConte nt[1][0]); FieldConte nt[2][0]);
The problem, of course, is that sscanf stops at a space, so you either have to break the remaining text (after the number) into several strings and the reassemble them, or carve off the number from the input string and parse it separately. Given your desire to use sscanf, I chose the approach below.
There are much better ways to do this sort of thing. Are you limited to sscanf for some reason? std::string offers facilities, CString (if you're using MS), etc. The Boost library offers string tokenizing, among other things.
#include <stdio.h>
void Scan(char* source, int* iResult, char* sResult) {
char term1[256], term2[256], term3[256], term4[256];
term1[0] = term2[0] = term3[0] = term4[0] = '\0';
sscanf(source,"%d%s %s %s %s",iResult,term1,term2,te
sprintf(sResult,"%s %s %s %s",term1,term2,term3,term
}
void main() {
char rec1[] = "1 Record One With Space";
char rec2[] = "2 RecordTwo";
char rec3[] = "3 Record Three with space";
int IntegerVar[3];
char FieldContent[3][256];
Scan(rec1,&IntegerVar[0],&
Scan(rec2,&IntegerVar[1],&
Scan(rec3,&IntegerVar[2],&
}