How to read a data file in C++,

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

  • : 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
  • : 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]
  • 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

  • : : 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
  • : 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
  • : : 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.
  • 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





  • : 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]
  • : 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.
  • : : 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
  • #include
    #include
    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 << "
    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
  • : #include
    : #include
    : 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 << "
    Number in file input.dat = " << temp;
    [red]//u r not taking input further from the file[/red]
    : }
    :
    : return 0;
    : }

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

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

    return 0;
    }
    [/code]
  • : : #include
    : : #include
    : : 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 << "
    Number in file input.dat = " << temp;
    : [red]//u r not taking input further from the file[/red]
    : : }
    : :
    : : return 0;
    : : }
    :
    : the code should be like this:
    : [code]
    : int main()
    : {
    : ifstream infile("infile.dat");
    : float temp;
    :
    : while(infile >> temp)
    : cout << temp << endl;
    :
    : return 0;
    : }
    : [/code]
    :


    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.

  • : : : #include
    : : : #include
    : : : 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 << "
    Number in file input.dat = " << temp;
    : : [red]//u r not taking input further from the file[/red]
    : : : }
    : : :
    : : : return 0;
    : : : }
    : :
    : : the code should be like this:
    : : [code]
    : : int main()
    : : {
    : : ifstream infile("infile.dat");
    : : float temp;
    : :
    : : while(infile >> temp)
    : : cout << temp << endl;
    : :
    : : return 0;
    : : }
    : : [/code]
    : :
    :
    :
    : 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.
    :
    :
    [green] 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.
    [/green]

  • : : : [code]
    : : : int main()
    : : : {
    : : : ifstream infile("infile.dat");
    : : : float temp;
    : : :
    : : : while(infile >> temp)
    : : : cout << temp << endl;
    : : :
    : : : return 0;
    : : : }
    : : : [/code]
    : : :
    : :
    : :
    : : 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.
    : :
    : :
    : [green] 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.
    : [/green]
    [purple]
    new line characters are not a problem for the above code, as far as the file is a text file.
    [/purple]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories