By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

how to read and write in text file

P: 2
I am trying to write and read entered text into a text file. I have managed to write into the text file but reading from it is proving a challenge. I can provide a snippet of code. and stud is an object of a class students. I'm using c++.

Expand|Select|Wrap|Line Numbers
  1. {
  2.  
  3.                     string str;
  4.                     int n, count = 0;
  5.  
  6.                 cout<<"Enter number of courses: ";
  7.                 cin>>n;
  8.  
  9.             for(int i=0; i<=n-1; i++);
  10.                     {
  11.  
  12.                     cout<<"Enter course: ";
  13.                     cin>>str;
  14.  
  15.  
  16. count=0;
  17.          for(int i=0; i<=arr; i++)    
  18.  
  19.          {         
  20.  
  21.          if(str == stud[i].cours)
  22.  
  23.          {        
  24. count++;
  25.  
  26. }
  27.  
  28. }
  29.  
  30. cout<<"Number of students doing "<<str<<" are "<<count<<endl;
  31. cfile<<"Number of students doing "<<str<<" are "<<count<<endl;
  32.  
  33. }
  34.  
  35. }
  36.  
  37.  
  38.  
  39.             cout<<"\n-----------------------------------------------------------------------------------------------------------------------------------------------"<<endl;
  40.             cfile<<"\n-----------------------------------------------------------------------------------------------------------------------------------------------"<<endl;
  41.                 for(int i=0; i<1; i++)
  42.                 {
  43.            cout<<setw(12)<<"Rollno ||";
  44.            cfile<<setw(12)<<"Rollno ||";
  45.                     cout<<setw(12)<<" Name   ||";
  46.                     cfile<<setw(12)<<" Name   ||";
  47.                     cout<<setw(12)<<" Surname    || ";
  48.                     cfile<<setw(12)<<" Surname    || ";
  49.                       cout<<setw(12)<<"Age  ||";
  50.                       cfile<<setw(12)<<"Age  ||";
  51.                       cout<<setw(12)<<" Course  ||";
  52.                       cfile<<setw(12)<<" Course  ||";
  53.                       cout<<setw(12)<<" Module   ||";
  54.                       cfile<<setw(12)<<" Module   ||";
  55.  
  56.                       for(int k=0; k<arr; k++)
  57.                       {
  58.                           if(stud[k].idno!='d')
  59.                           {
  60.                           cout<<"\n";
  61.                           cfile<<"\n";
  62.                               cout<<" ";
  63.                                   cfile<<" ";
  64.                               cout<<setw(5)<<stud[k].idno;
  65.                               cfile<<setw(5)<<stud[k].idno;
  66.  
  67.                               cout<<setw(15)<<stud[k].name;
  68.                               cfile<<setw(15)<<stud[k].name;
  69.  
  70.                           cout<<setw(14)<<stud[k].sname;
  71.                           cfile<<setw(14)<<stud[k].sname;
  72.  
  73.  
  74.                            cout<<setw(14)<<stud[k].age;
  75.                            cfile<<setw(14)<<stud[k].age;
  76.  
  77.                             cout<<setw(12)<<stud[k].cours;
  78.                             cfile<<setw(12)<<stud[k].cours;
  79.  
  80.                             cout<<setw(14)<<stud[k].modules;
  81.                             cfile<<setw(14)<<stud[k].modules;
  82.  
  83.                             }
  84.                         }
  85.              }
  86.             cout<<"\n-----------------------------------------------------------------------------------------------------------------------------------------------"<<endl;
  87.             cfile<<"\n-----------------------------------------------------------------------------------------------------------------------------------------------"<<endl;
  88.             cfile.write((char *)&stud, sizeof(stud));
  89.             cfile.read((char *)&stud, sizeof(stud));
  90.             cfile.close();
  91.  
1 Week Ago #1
Share this Question
Share on Google+
1 Reply


dev7060
100+
P: 117
Reading can be done the same way as writing was done into the file. What exactly is not working?

I suggest to post the code after fixing the indentation and errors. Also, mention what snippet is under what function.
5 Days Ago #2

Post your reply

Sign in to post your reply or Sign up for a free account.