-
May 12th, 2013, 01:01 PM
#1
Read from file
Hello.
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?
Code:
#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;
}
inputFile.close();
return 0;
}
Last edited by ted_kingdom; May 12th, 2013 at 01:26 PM.
-
May 12th, 2013, 02:21 PM
#2
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 :
Code:
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 12th, 2013, 02:35 PM
#3
Re: Read from file
Thank you for your answer, but I went a little bit different way:
Code:
#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];
i++;
}
}
latticeType = inputData[0];
d = inputData[1];
h = inputData[2];
dimX = inputData[3];
dimY = inputData[4];
dimZ = inputData[5];
file.close();
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|