LinuxQuestions.org
Visit Jeremy's Blog.
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 09-07-2009, 10:20 PM   #1
Mork2k4
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Rep: Reputation: 0
Reading variables from a text file in C++


Forgive me if there is another thread like this one but I did not see a thread that matched what I was looking for.

I can read lines from a text file easy but I do not know how to read a single variable and store it to an array. For example:

1 1 1 0 0 0 1 0 0 1 0 1 1
0 0 1 0 0 1 1 1 0 0 1 0 0

I can read the whole thing but I need to store it into a 2d array. I am having problems reading one number at a time.
 
Old 09-08-2009, 02:33 AM   #2
alexroux
LQ Newbie
 
Registered: Sep 2009
Location: Lausanne (Switzerland)
Distribution: Ubuntu
Posts: 12

Rep: Reputation: 3
Two common ways:

In C: using fscanf (http://www.cplusplus.com/reference/c...cstdio/fscanf/)
In C++: using ifstream and >> operator (http://www.cplusplus.com/reference/iostream)/istream/operator%3E%3E/)

I advise to use the C++ way since you code in C++.
 
Old 09-08-2009, 03:08 AM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Code:
#include <cstring>
#include <fstream>
int main()
{
	const size_t Sx = 13, Sy = 2;
	int a[Sx][Sy];
	size_t x = 0, y = 0;
	std::ifstream file("test.dat");
	while (file)
	{
		file >> a[x][y];
		if (++x == Sx)
		{
			x = 0;
			if (++y == Sy)
				y = 0;
		}
	}
	file.close();
}
 
Old 09-08-2009, 05:05 AM   #4
hyperactive22
LQ Newbie
 
Registered: May 2007
Posts: 8

Rep: Reputation: 0
@neonsignal

Could u please simulate ur code for the input given by OP. I found that the contents stored in array are as follows:
1 0 1 0 1 1 0 0 0 0 0 1 1
1 0 1 1 0 0 0 0 0 1 1 1 0
 
Old 09-08-2009, 05:24 AM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
Quote:
Could u please simulate ur code for the input given by OP. I found that the contents stored in array are as follows:
1 0 1 0 1 1 0 0 0 0 0 1 1
1 0 1 1 0 0 0 0 0 1 1 1 0
Try switching around the array indexes when you print the array out.
 
Old 09-08-2009, 05:36 AM   #6
hyperactive22
LQ Newbie
 
Registered: May 2007
Posts: 8

Rep: Reputation: 0
Seems that the problem was due to newline character in file. Modified the code given by neonsignal a little bit and swapped the index checks. Its working now.
int main()
{
const size_t Sx = 13, Sy = 2;
int a[Sx][Sy];
size_t x = 0, y = 0;
std::ifstream file("test.dat");
while (file)
{
file >> a[x][y];
if (++y == Sx)
{
y = 0;
++x ;
file ; //This line is to read the newline char and discard it
}
}
file.close();
return 0;
}
 
Old 09-08-2009, 05:39 AM   #7
hyperactive22
LQ Newbie
 
Registered: May 2007
Posts: 8

Rep: Reputation: 0
Seems to be a problem with newline char in file. Modified the code posted by neonsignala bit and swapped the indices, now the code is working.
int main()
{
const size_t Sx = 13, Sy = 2;
int a[Sx][Sy];
size_t x = 0, y = 0;
std::ifstream file("test.dat");
while (file)
{
file >> a[x][y];
if (++y == Sx)
{
y = 0;
++x ;
file ; //This line reads and discards the newline character
}
}
file.close();
return 0;
}
 
Old 09-09-2009, 09:54 AM   #8
Mork2k4
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Original Poster
Rep: Reputation: 0
That worked perfectly. Thank's
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Python Reading and Declaring Variables from a Text File dudeman41465 Programming 7 01-31-2009 04:54 PM
reading variables from other file RudraB Programming 1 03-28-2008 06:06 AM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
Reading text file-writting binary file cdog Programming 5 06-13-2006 11:56 AM
BASH script Ė reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM


All times are GMT -5. The time now is 03:12 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration