Hye

I want to store my teachers name, course name in file. But the names can also include spaces...

using getline() func I can store the string with spaces in to file...
But while reading I want that instead of considering space as an indication of new string... there shd be \n or enter key....

But its nt working as desired....

Plz help me....

Sid

code is....

#include <stdio.h>
#include<conio.h>
#include<iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>

const int max=10;
struct abc
{
  char  t_name[30];
  char  c_name[30];
  int   c_hours;
  } ;
  abc obj[max],temp[max];
  
 class time_table
 {
 public:
	 void tchr_n (void)
	 {
	
		 ofstream fout("SIDRA.txt");

        if(!fout.is_open())
		{
           cerr<<  "cant open file"<<endl;
 
		}
cout<<"Enter teacher's name  ";
  for(int i=0; i<6;i++)
  {
    
    cin.getline(obj[i].t_name,30,'\n');
    fout<<obj[i].t_name<<"\n";
          
  }
          fout.close();

	 }

	 void disp_n(void)
	 {
		 ifstream fin("SIDRA.txt");
   for(int j=0; j<6;j++) 
   {		 
	   while(!fin.getline(obj[j].t_name, 20).eof())
	   {
		  
	 cout<<obj[j].t_name<<endl;

	   }
fin>>obj[j].t_name;
 }



cout<<obj[5].t_name;
cout<<obj[3].t_name;		  
		  

  fin.close();
	 }
 
 };

 void main()
  {

	  time_table rec[max];
	  cout<<"Enter teacher name "<<endl;
                 rec[0].tchr_n();
	  rec[0].disp_n();	
  getche();
  }

Recommended Answers

All 3 Replies

Use write method.

can u briefly describe this read and write method
in two or three lines just syntax....

I have been stuck in this part of code for 2 days...
I have to carry on wwith the project..

use cin.get(). It reads chars including spaces.

Try something like this

ifstream iFile("sample.txt");
char c;
while( iFile.get(c) ) 
{ 
    if(c == ' ') //add to new string
    else addToTheLastString .
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.