C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to read a data file in C++, Posted by amol12882 on 28 Sept 2005 at 6:15 AM
Hello everyone,
I am facing problem to read a ASCII data file, the format of my file is,
----------------Input.dat---------------
Statements.............
Statements.............
Statements.............
A B C D E
1.0 2.36 5.23 5.1 4.6
2.0 1.36 7.23 4.1 2.6
3.0 2.36 8.23 3.1 34.6
4.0 3.36 2.23 5.1 4.6
5.0 2.36 5.23 1.1 2.6
........................................
........................................

up to NULL character,
Here there are five coloums. A,B,C,D and E.
I am programming on Linux System.
Will anyone help to resolve this problem,

Regards
amol12882

Report
Re: How to read a data file in C++, Posted by CyGuy on 28 Sept 2005 at 6:18 AM
: Hello everyone,
: I am facing problem to read a ASCII data file, the format of my file is,
: ----------------Input.dat---------------
: Statements.............
: Statements.............
: Statements.............
: A B C D E
: 1.0 2.36 5.23 5.1 4.6
: 2.0 1.36 7.23 4.1 2.6
: 3.0 2.36 8.23 3.1 34.6
: 4.0 3.36 2.23 5.1 4.6
: 5.0 2.36 5.23 1.1 2.6
: ........................................
: ........................................
:
: up to NULL character,
: Here there are five coloums. A,B,C,D and E.
: I am programming on Linux System.
: Will anyone help to resolve this problem,
:
: Regards
: amol12882
:
:
I only know ANSI, and not using the null, instead use EOF
Report
Re: How to read a data file in C++, Posted by HK_MP5KPDW on 28 Sept 2005 at 7:02 AM
: Hello everyone,
: I am facing problem to read a ASCII data file, the format of my file is,
: ----------------Input.dat---------------
: Statements.............
: Statements.............
: Statements.............
: A B C D E
: 1.0 2.36 5.23 5.1 4.6
: 2.0 1.36 7.23 4.1 2.6
: 3.0 2.36 8.23 3.1 34.6
: 4.0 3.36 2.23 5.1 4.6
: 5.0 2.36 5.23 1.1 2.6
: ........................................
: ........................................
:
: up to NULL character,
: Here there are five coloums. A,B,C,D and E.
: I am programming on Linux System.
: Will anyone help to resolve this problem,
:
: Regards
: amol12882
:
:

Well, what do you need to do with the data once you've read it? How you read it in depends on what you're going to be doing with it. For example, if you just need to spit it back out to the user, you can just read in a line at a time into a buffer using one of the getline functions (if using C++) or the fgets function (if using C) and then just output what you read back to the console. If you need to do something different like store the data in arrays for further manipulation, then you need to do something different with how you read the data into your program.
Report
Re: How to read a data file in C++, Posted by amol12882 on 28 Sept 2005 at 8:46 PM
: : Hello everyone,
: : I am facing problem to read a ASCII data file, the format of my file is,
: : ----------------Input.dat---------------
: : Statements.............
: : Statements.............
: : Statements.............
: : A B C D E
: : 1.0 2.36 5.23 5.1 4.6
: : 2.0 1.36 7.23 4.1 2.6
: : 3.0 2.36 8.23 3.1 34.6
: : 4.0 3.36 2.23 5.1 4.6
: : 5.0 2.36 5.23 1.1 2.6
: : ........................................
: : ........................................
: :
: : up to NULL character,
: : Here there are five coloums. A,B,C,D and E.
: : I am programming on Linux System.
: : Will anyone help to resolve this problem,
: :
: : Regards
: : amol12882
: :
: :
:
: Well, what do you need to do with the data once you've read it? How you read it in depends on what you're going to be doing with it. For example, if you just need to spit it back out to the user, you can just read in a line at a time into a buffer using one of the getline functions (if using C++) or the fgets function (if using C) and then just output what you read back to the console. If you need to do something different like store the data in arrays for further manipulation, then you need to do something different with how you read the data into your program.
:

I Have to do the computations with that. I have already wrote a program to generate data, now I want that data for my further computations. As I am doing PhD in Theoritical Physics, the generation of data itself took lot of time. Hence first I will generate data and then do the computations with the available data.

Regards
amol12882
Report
Re: How to read a data file in C++, Posted by bilderbikkel on 28 Sept 2005 at 9:09 AM
For an example using our beloved STL's:

http://www.codepedia.com/1/CppFileIo

It has some other usefull code snippets for basic file I/O.

Good luck,
bilderbikkel

Report
Re: How to read a data file in C++, Posted by CyGuy on 28 Sept 2005 at 9:59 PM
: For an example using our beloved STL's:
:
: http://www.codepedia.com/1/CppFileIo
:
: It has some other usefull code snippets for basic file I/O.
:
: Good luck,
: bilderbikkel
:
:
that is awsome code to learn from, simple and to the point... i like it!! the format of your file depends on how you write it. 0-31 in ascii are control characters. you can type them directly by holding ALT and pressing 0-3-1 release ALT in the editor. I would also suggest making a class/object containing the 5 variables on each line. here is a link for more on ascii: http://computing-dictionary.thefreedictionary.com/ascii
Report
Re: How to read a data file in C++, Posted by Lundin on 28 Sept 2005 at 11:29 PM
: : For an example using our beloved STL's:
: :
: : http://www.codepedia.com/1/CppFileIo
: :
: : It has some other usefull code snippets for basic file I/O.
: :
: : Good luck,
: : bilderbikkel
: :
: :
: that is awsome code to learn from, simple and to the point... i like it!! the format of your file depends on how you write it. 0-31 in ascii are control characters. you can type them directly by holding ALT and pressing 0-3-1 release ALT in the editor. I would also suggest making a class/object containing the 5 variables on each line. here is a link for more on ascii: http://computing-dictionary.thefreedictionary.com/ascii
:


It don't think it is good practice to write the characters with ALT.
You might get into trouble when the system is using another symbol table. That is why we have \0 \n \r etc.
To get character 31, either type

char ch=31;
or
char ch='\x1F';

and place a comment of which symbol it is, instead.
Report
Re: How to read a data file in C++, Posted by amol12882 on 29 Sept 2005 at 6:15 AM
I want to read a data and not a strings as I have to do the further cpmputations with that.

I have used the following code to read a data,

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
ifstream infile("input.dat");
float temp;

infile >> temp; // Assuming "input.dat" exists

cout << "\n Number in file input.dat = " << temp;

return 0;
}
Here is my input.dat file is,
...................input.dat.............
12.55 8.99
12.68 7.56
2.655 7.1
..........................................

Now the problem is I can not read the data in next lines eg 12.68, 7.56 etc. But I can read data of first line only.
Plese help me..

Regards
amol12882





Report
Re: How to read a data file in C++, Posted by Lundin on 29 Sept 2005 at 7:01 AM
: I want to read a data and not a strings as I have to do the further cpmputations with that.

You are acctually using strings as long as you don't set the input/output to be binary.


:
: I have used the following code to read a data,
:
: #include <fstream>
: #include <iostream>
: using namespace std;
:
: int main()
: {
: ifstream infile("input.dat");
: float temp;
:
: infile >> temp; // Assuming "input.dat" exists
:
: cout << "\n Number in file input.dat = " << temp;
:
: return 0;
: }
: Here is my input.dat file is,
: ...................input.dat.............
: 12.55 8.99
: 12.68 7.56
: 2.655 7.1
: ..........................................
:
: Now the problem is I can not read the data in next lines eg 12.68, 7.56 etc. But I can read data of first line only.

Can you post the full code?
Report
Re: How to read a data file in C++, Posted by amol12882 on 29 Sept 2005 at 8:53 PM
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream infile("input.dat");
float temp;

infile >> temp; // Assuming "input.dat" exists

for(int i=0;i<=2;i++)
{
cout << "\n Number in file input.dat = " << temp;
}

return 0;
}
Here is my input.dat file is,
...................input.dat.............
12.55 8.99
12.68 7.56
2.655 7.1
.........................................


This code can read the data only in first line and not in other lines,
if numbers are seperated by space then this code can be used to read the data from first line only.
How I can recognize the new line charactar?
Regards
amol12882
Report
Re: How to read a data file in C++, Posted by Donotalo on 29 Sept 2005 at 9:39 PM
: #include <fstream>
: #include <iostream>
: using namespace std;
: int main()
: {
: ifstream infile("input.dat");
: float temp;
:
: infile >> temp; // Assuming "input.dat" exists
:
: for(int i=0;i<=2;i++)
: {
: cout << "\n Number in file input.dat = " << temp;
//u r not taking input further from the file
: }
:
: return 0;
: }

the code should be like this:
int main()
{
	ifstream infile("infile.dat");
	float temp;

	while(infile >> temp)
		cout << temp << endl;

	return 0;
}

Report
Re: How to read a data file in C++, Posted by Lundin on 29 Sept 2005 at 11:14 PM
: : #include <fstream>
: : #include <iostream>
: : using namespace std;
: : int main()
: : {
: : ifstream infile("input.dat");
: : float temp;
: :
: : infile >> temp; // Assuming "input.dat" exists
: :
: : for(int i=0;i<=2;i++)
: : {
: : cout << "\n Number in file input.dat = " << temp;
: //u r not taking input further from the file
: : }
: :
: : return 0;
: : }
:
: the code should be like this:
:
: int main()
: {
: 	ifstream infile("infile.dat");
: 	float temp;
: 
: 	while(infile >> temp)
: 		cout << temp << endl;
: 
: 	return 0;
: }
: 

:


Oh, the first code was the real one? Yes, it is missing a loop. You are only reading one value from the file. Regarding newline and other whitespaces, ifstream should sort them away for you. Or you can use getline() if you prefer that.

Report
Re: How to read a data file in C++, Posted by amol12882 on 30 Sept 2005 at 1:04 AM
: : : #include <fstream>
: : : #include <iostream>
: : : using namespace std;
: : : int main()
: : : {
: : : ifstream infile("input.dat");
: : : float temp;
: : :
: : : infile >> temp; // Assuming "input.dat" exists
: : :
: : : for(int i=0;i<=2;i++)
: : : {
: : : cout << "\n Number in file input.dat = " << temp;
: : //u r not taking input further from the file
: : : }
: : :
: : : return 0;
: : : }
: :
: : the code should be like this:
: :
: : int main()
: : {
: : 	ifstream infile("infile.dat");
: : 	float temp;
: : 
: : 	while(infile >> temp)
: : 		cout << temp << endl;
: : 
: : 	return 0;
: : }
: : 

: :
:
:
: Oh, the first code was the real one? Yes, it is missing a loop. You are only reading one value from the file. Regarding newline and other whitespaces, ifstream should sort them away for you. Or you can use getline() if you prefer that.
:
:
I am sorry, In hurry I have just missed that loop, but I mean to say the same as you said. Problem is of newline charactor.


Report
Re: How to read a data file in C++, Posted by Donotalo on 30 Sept 2005 at 2:06 AM
: : :
: : : int main()
: : : {
: : : 	ifstream infile("infile.dat");
: : : 	float temp;
: : : 
: : : 	while(infile >> temp)
: : : 		cout << temp << endl;
: : : 
: : : 	return 0;
: : : }
: : : 

: : :
: :
: :
: : Oh, the first code was the real one? Yes, it is missing a loop. You are only reading one value from the file. Regarding newline and other whitespaces, ifstream should sort them away for you. Or you can use getline() if you prefer that.
: :
: :
: I am sorry, In hurry I have just missed that loop, but I mean to say the same as you said. Problem is of newline charactor.
:


new line characters are not a problem for the above code, as far as the file is a text file.

Report
Re: How to read a data file in C++, Posted by amol12882 on 30 Sept 2005 at 5:56 AM
: : #include <fstream>
: : #include <iostream>
: : using namespace std;
: : int main()
: : {
: : ifstream infile("input.dat");
: : float temp;
: :
: : infile >> temp; // Assuming "input.dat" exists
: :
: : for(int i=0;i<=2;i++)
: : {
: : cout << "\n Number in file input.dat = " << temp;
: //u r not taking input further from the file
: : }
: :
: : return 0;
: : }
:
: the code should be like this:
:
: int main()
: {
: 	ifstream infile("infile.dat");
: 	float temp;
: 
: 	while(infile >> temp)
: 		cout << temp << endl;
: 
: 	return 0;
: }
: 

:

Then what can I do to read the numbers at the new line?How one can modify the code as shown above to read two numbers which are in two diffrent lines?


Report
Re: How to read a data file in C++, Posted by Donotalo on 30 Sept 2005 at 7:35 AM
: : : #include <fstream>
: : : #include <iostream>
: : : using namespace std;
: : : int main()
: : : {
: : : ifstream infile("input.dat");
: : : float temp;
: : :
: : : infile >> temp; // Assuming "input.dat" exists
: : :
: : : for(int i=0;i<=2;i++)
: : : {
: : : cout << "\n Number in file input.dat = " << temp;
: : //u r not taking input further from the file
: : : }
: : :
: : : return 0;
: : : }
: :
: : the code should be like this:
: :
: : int main()
: : {
: : 	ifstream infile("infile.dat");
: : 	float temp;
: : 
: : 	while(infile >> temp)
: : 		cout << temp << endl;
: : 
: : 	return 0;
: : }
: : 

: :
:
: Then what can I do to read the numbers at the new line?How one can modify the code as shown above to read two numbers which are in two diffrent lines?
:


you dont need any modification for that. the above code works for u.
Report
Re: How to read a data file in C++, Posted by EPenguin on 29 Sept 2005 at 7:53 AM
: I want to read a data and not a strings as I have to do the further cpmputations with that.
:
: I have used the following code to read a data,
:
: #include <fstream>
: #include <iostream>
: using namespace std;
:
: int main()
: {
: ifstream infile("input.dat");
: float temp;
:
: infile >> temp; // Assuming "input.dat" exists
:
: cout << "\n Number in file input.dat = " << temp;
:
: return 0;
: }
: Here is my input.dat file is,
: ...................input.dat.............
: 12.55 8.99
: 12.68 7.56
: 2.655 7.1
: ..........................................
:
: Now the problem is I can not read the data in next lines eg 12.68, 7.56 etc. But I can read data of first line only.
: Plese help me..
:
: Regards
: amol12882
:
:
:
From the looks of things all you need is a loop. It could also be you're not getting the new line character.
Report
Re: How to read a data file in C++, Posted by amol12882 on 29 Sept 2005 at 8:39 PM
: : I want to read a data and not a strings as I have to do the further cpmputations with that.
: :
: : I have used the following code to read a data,
: :
: : #include <fstream>
: : #include <iostream>
: : using namespace std;
: :
: : int main()
: : {
: : ifstream infile("input.dat");
: : float temp;
: :
: : infile >> temp; // Assuming "input.dat" exists
: :
: : cout << "\n Number in file input.dat = " << temp;
: :
: : return 0;
: : }
: : Here is my input.dat file is,
: : ...................input.dat.............
: : 12.55 8.99
: : 12.68 7.56
: : 2.655 7.1
: : ..........................................
: :
: : Now the problem is I can not read the data in next lines eg 12.68, 7.56 etc. But I can read data of first line only.
: : Plese help me..
: :
: : Regards
: : amol12882
: :
: :
: :
: From the looks of things all you need is a loop. It could also be you're not getting the new line character.
:
Loop is not a problem, but I am unable to recognize the new line charactar. I am unable to read the data from next line. But a for loop can be used to read the data from one line if it is seprated by the tabs or space.
Regards
amol12885



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.