CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Read from file

  1. #1
    Join Date
    Apr 2013

    Read from file

    I want my program to read variables from file "input.txt":
    Please choose the crystal lattice. Type 1 for BCC, 2 for FCC, and 3 for HCP: 2
    Please enter lattice parameter (a): 1
    For HCP please enter second lattice parameter (c): 1
    Please enter the number of translated cells along X axis: 2
    Please enter the number of translated cells along Y axis: 2
    Please enter the number of translated cells along Z axis: 2
    Moreover, I want program to read only the numbers after colon. How can I do it?
    #include <iostream>#include <fstream> // to read from file
    using namespace std;
    int main ()
        int latticeType;
        int dimX, dimY, dimZ; // number of translated cells along each axis
        float d; // lattice parameter
        float h; // second lattice parameter for HCP
        ifstream inputFile; // object of class 'ifstream' to read from it
        inputFile.open("input.txt", ios::in); // open 'input.txt' only for reading
        if (!inputFile){
            cerr << "Unable to open input file";
            return -1;
        while (!inputFile.eof()){
            inputFile >> latticeType;
            cout << latticeType << endl;
        if ( (latticeType!=1) && (latticeType!=2) && (latticeType!=3) ){
            cerr << "Invalid value of lattice type" << endl;
            return -1;
            inputFile >> d;
            cout << d << endl;
            if (latticeType==3){
                inputFile >> h;
                cout << d << endl;
            inputFile >> dimX;
            cout << dimX;
            inputFile >> dimY;
            cout << dimY;
            inputFile >> dimZ;
            cout << dimZ;
        return 0;
    Last edited by ted_kingdom; May 12th, 2013 at 01:26 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: Read from file

    Possibly the easiest way is to read a complete line at a time from the file then parse for the string to the right of the :

    string fline;
    size_t coln;
    int  val;
    	getline(inputFile, fline);
    	if ((coln = fline.find_last_of(":")) != string::npos) {
    		val = atoi((fline.substr(coln + 1, fline.length() - coln)).c_str());
                    cout << "val: " << val << endl;
    This sample will read a line from the file and display the value of the integer to the right of the last : in the line. As you need to read multiple lines, this could be made into a function.

    Hope this helps.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.4)

  3. #3
    Join Date
    Apr 2013

    Re: Read from file

    Thank you for your answer, but I went a little bit different way:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
        float inputData [6];
        int i=0;
        ifstream file("input.txt");
        char c;
        while (file.good()) {
            c = file.get();
            if (c == ':') {
                file >> inputData[i];
        latticeType = inputData[0];
        d = inputData[1];
        h = inputData[2];
        dimX = inputData[3];
        dimY = inputData[4];
        dimZ = inputData[5];
        return 0;
    Last edited by ted_kingdom; May 12th, 2013 at 02:37 PM.

Tags for this Thread

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.