LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2005, 04:08 PM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Using sscanf to grab an unknown number of variables


I have files containing floating point data that is tab-delimted. I need to read in these files to fill a 2 dimensional vector. The problem here is that I don't know how many floating point data there are per-line in the file (however the number of entries are consistent within the file). So I wanted to use getline to grab each new line of data and then use sscanf to parse the data, but I'm stuck on how to do this. Here's what I've got written so far.

Code:
// >>>>> (1) Try to open the input file for reading
	ifstream fin(input_filename.c_str());
	if(!fin) {
		cerr << "ERROR: Problem opening file " << input_filename << " for reading." << endl;
		fin.close();
		exit(1);
	}

	// >>>>> (2) Read in the first line and determine how many PMC data entries
	// >>>>>     are in the file
	char ch;
	int pmc_entries = 0;
	do {
		fin.get(ch);
		if (fin.bad()) {
			cerr << "ERROR: Problem reading file " << input_filename << endl;
			fin.close();
			exit(1);
		}

		if (ch == '\t' || ch == '\n') {
			pmc_entries++;
		}
	} while (ch != '\n');

	cout << "File has " << pmc_entries << " pmc entries" << endl;
	vector<vector<float> > pmc_data(pmc_entries);

	// >>>>> (3) Return to the beginning of the file and read in each line of
	// >>>>>     data into the pmc_data vector <<<<<
	int i = 0;
	int v = 0;    // A  << counter for the pmc_data vector element to fill
	char line[1000];

	fin.seekg(0); // Go back to the beginning of the file

	while (fin.good()) {
		fin.getline(line, 1000);
		for (i = 0; i < pmc_entries; i++) {
			sscanf(line,"%f\t",&pmc_data[v][i]);
		}
	}

The problem is with the sscanf line inside the inner for loop. It will continually read the same floating point number (the first one in the file) every loop. It would be simple enough for me to construct a string that consisted of multiple "%f\t" for the exact number of entries, but how can I specify where each of those values go (in the pmc_data[][] vector)? Could I use the elipse (...) operator to somehow fill the first dimension of that vector? Thanks for any ideas you may have
 
Old 06-06-2005, 04:34 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Read the first line, count the tabs. That gives you the number of fields for every line.
 
Old 06-06-2005, 04:49 PM   #3
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by jim mcnamara
Read the first line, count the tabs. That gives you the number of fields for every line.

I already do that in the code I posted above. That's not the problem. The problem is how to use sscanf to read in an arbitrary number of floating point values that aren't known until run-time.
 
Old 06-06-2005, 04:55 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
You can't. You'll have to use varargs.h to call a routine that accepts the number of items then branches to a sscanf call with the right number of arguments.

It would be easier to read and maintain if you just copied each substring (ending in a tab) to a temp string, then called sscanf for a single value
 
Old 06-06-2005, 05:03 PM   #5
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
That's what I thought, ok thanks. I'll go with your substring split idea.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read /proc/net/dev with sscanf? Thinking Linux - General 0 03-30-2005 09:14 AM
another sscanf Q jnusa Programming 10 10-27-2004 02:33 AM
Garbage value dumped by sscanf sheenak Programming 2 05-27-2004 11:55 PM
sscanf help jpc82 Programming 8 11-04-2003 02:01 PM
glibc patch for sscanf.c jarin scott Linux - Software 2 08-03-2003 02:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration