Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to read a data file in C++,

amol12882amol12882 Member Posts: 7
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

«1

Comments

  • CyGuyCyGuy Member Posts: 312
    : 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
  • HK_MP5KPDWHK_MP5KPDW Member Posts: 770 ✭✭✭
    : 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
    :
    :

    [blue]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 [italic]getline[/italic] functions (if using C++) or the [italic]fgets[/italic] 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.[/blue]
  • bilderbikkelbilderbikkel Member Posts: 754
    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

  • amol12882amol12882 Member Posts: 7
    : : 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
    : :
    : :
    :
    : [blue]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 [italic]getline[/italic] functions (if using C++) or the [italic]fgets[/italic] 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.[/blue]
    :

    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
  • CyGuyCyGuy Member Posts: 312
    : 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
  • LundinLundin Member Posts: 3,711
    : : 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

    etc.
    To get character 31, either type

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

    and place a comment of which symbol it is, instead.
  • amol12882amol12882 Member Posts: 7
    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
    #include
    using namespace std;

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

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

    cout << "
    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





  • LundinLundin Member Posts: 3,711
    : I want to read a data and not a strings as I have to do the further cpmputations with that.

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

    :
    : I have used the following code to read a data,
    :
    : #include
    : #include
    : using namespace std;
    :
    : int main()
    : {
    : ifstream infile("input.dat");
    : float temp;
    :
    : infile >> temp; // Assuming "input.dat" exists
    :
    : cout << "
    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.

    [blue]Can you post the full code?[/blue]
  • EPenguinEPenguin Member Posts: 23
    : 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
    : #include
    : using namespace std;
    :
    : int main()
    : {
    : ifstream infile("input.dat");
    : float temp;
    :
    : infile >> temp; // Assuming "input.dat" exists
    :
    : cout << "
    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.
  • amol12882amol12882 Member Posts: 7
    : : 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
    : : #include
    : : using namespace std;
    : :
    : : int main()
    : : {
    : : ifstream infile("input.dat");
    : : float temp;
    : :
    : : infile >> temp; // Assuming "input.dat" exists
    : :
    : : cout << "
    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
«1
Sign In or Register to comment.