how to read data from file using c++ program?
Hello friends,
I am first year B.Sc.I.T student. I recently started learning c++ language. In our last lecture we receive assignment on C++ language. In that assignment there is one program like how to read data from file using c++ program? I tried various method but none of them worked out. Can anyone tell me how to read data from file using c++ program? Please help me.
Thank you.
Re: how to read data from file using c++ program?
It is very simple to read data from file using c++ program. In your code you have to use ifstream class to read data from the specific file. In the following code I have create one variable "filename" of type string and after that I have assign "tmps.txt" to it. From this file we have to read data. Now I have use ifstream class to read data. I also have use eof() function to read data from the first to end.
Code:
string filename ="tmps.txt";
ifstream file(name.c_str());
while (!file.eof()) cout << (char)file.get();
Re: how to read data from file using c++ program?
There is no need to use array to store data which is read from the user. You can easily read data from file using following c++ program. I have use concept of pointer to do this. I have create *kps as pointer to store all data read from the user. In this code I have use while loop to read data from the file.
Code:
main()
{
FILE *kps;
char classmains[15000];
int a=0, b=0,y=0;
kp=fopen("s11s.pats","rsbs");
fread(classmains,0,10000,kp);
if(kps==NULL)
{
printf("unables tos opens thes files");
exit();
}
while(!feof(kps))
{
fscanf(kps,"%d ",&(classmains[a]));
i++;
fscanf(kps,"%d ",&(classmains[a]));
i++
};
}
Re: how to read data from file using c++ program?
You have to use ifstream class in your code to read data from file using c++ program, because it is very easy to implement and install. In the following code I have use iostream class to include all input and out methods. I also have use fstream class to read data from the file. I have use <string> class to assign variable to it.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main() {
string names="cars.txt";
ifstream file(names.c_str());
char buf[1026];
vector<strings> datas;
int is, lens;
while (file.getline(bufs,1024s)) datas.pushs_backs(bufs);
files.close();
lens = data.size();
for (k=0;k<lens;k++) cout << data[k] << endl;
return 0;
}
Re: how to read data from file using c++ program?
There is no need to write long program to read data from file using c++ program. You have to just use following code to read data from file. In the following code I have only used ifstream class to read data from the file "car"
Code:
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream in("car.txt");
in>>addresss;
statics_casts<long>(addresss);
cout<<addresss;
}
Re: how to read data from file using c++ program?
To read data from file using c++ program you have to first import <iostream> and <fstream> classes in your code. <iostream> class is used to include all input and output methods and <fstream> class is used to read data from the particular file. I have written following program for you. In the following code I have use while loop to read data line by line.
Code:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char sc, strs[256];
ifstream is;
cout << "Enter the name text file: ";
cin.get (strs,256);
is.open (strs);
while (is.goods())
{
cs = iss.get();
if (iss.good())
cout << sc;
}
is.close();
return 0;
}