CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2012
    Posts
    42

    reading data character by character from text file

    doubles values are stored in text file. 23.5 36.8 34.2 my teacher has advised me to read them character by character and then make words, like i have to read "2" "3" "." "5" and now have to make it or treat it as word and then using atoi() i have to convert it into double. but i dont know how to do this could anyone plz explain with some code relevant to my example. im new to filing so pardon me for basic questioning

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: reading data character by character from text file

    Can be done, but why?

    Why not just read the floating values:
    Code:
       std::ifstream input("test.txt");
       double d;
       while (input >> d) {
          std::cout << "Read " << d << "\n";
       }
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jul 2012
    Posts
    42

    Re: reading data character by character from text file

    following errors incured
    error C2065: 'input' : undeclared identifier
    E:\MyProjects\practice\practice.cpp(54) : error C2296: '>>' : illegal, left operand has type 'double'
    E:\MyProjects\practice\practice.cpp(54) : error C2297: '>>' : illegal, right operand has type 'double'
    Last edited by tspga; July 25th, 2012 at 10:57 AM.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: reading data character by character from text file

    It would help if you showed us the code too.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2012
    Posts
    42

    Re: reading data character by character from text file

    double d;
    //char ch;

    ifstream file("E:\\abc.txt");
    if(!file)
    {
    cout<<"error";
    exit(1);
    }

    while(input>>d)
    {

    std::cout<<d;
    }
    file.close();
    getch();

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: reading data character by character from text file

    You declared your ifstream to be named file, then you used the name input. You should be consistent.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured