Results 1 to 6 of 6

Thread: how to read data from file using c++ program?

  1. #1
    Join Date
    Nov 2009
    Posts
    67

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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();

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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++
     };
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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;
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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;
    
         }

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    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;
    }

Similar Threads

  1. Read from a single data file with multiple processors
    By Bayougoula in forum Motherboard Processor & RAM
    Replies: 8
    Last Post: 26-09-2010, 05:47 AM
  2. How to read string from a given text file using C# program?
    By Kasper in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 10:18 PM
  3. How to write string data to file using java program?
    By Linoo in forum Software Development
    Replies: 5
    Last Post: 21-01-2010, 09:26 PM
  4. [VB.Net] Rename File to Program with Data in Excel
    By machok in forum Software Development
    Replies: 5
    Last Post: 04-02-2009, 09:43 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,334,350.07901 seconds with 17 queries