I need help in file handling.

The problem is...

In a text file.. consists of (5) five student load with their...(subject,description,units,teacher,room,time, and day)..
I did the first student load.. but I could hardly proceed on the next student loads..
I easily did this in Java.. but on C++ i could hardly get the code..
Any help is very appreciated..

the text file looks like this:
[IMG]http://i164.photobucket.com/albums/u4/kenzo1588/notepad.jpg[/IMG]

my current output right now is:
[IMG]http://i164.photobucket.com/albums/u4/kenzo1588/SPL.jpg[/IMG]

my current code right now:

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <stdio.h>
#include <conio.h>
#include <iomanip>
int main()
{
using std::atoi;
std::ifstream File("NEW.txt");
int ctr;
static char subj[150];
int sum = 0;
char desc[150];
char unit[150];
char tch[150];
char rm[150];
char tym[150];
char day[150];
char sb[15] = "Subject";
char ds[12] = "Description";
char un[6] = "Units";
char tc[8] = "Teacher";
char room[5] = "Room";
char tm[5] = "Time";
char dy[4] = "Day";
printf("\n-------------------------------------------------------------------------------------------------\n");
printf("%-12s%-20s%-7s%-14s%-11s%-18s%-18s", sb,ds,un,tc,room,tm,dy);
printf("\n-------------------------------------------------------------------------------------------------\n");
for(ctr = 0; ctr<5;ctr++)
{
File.getline(subj,20,',');
File.getline(desc,32,',');
File.getline(unit,5,',');
int i = atoi(unit);
File.getline(tch,30,',');
File.getline(rm,20,','); 
File.getline(tym,20,',');
File.getline(day,15,',');
printf("%-13s", subj);
printf("%-20s", desc);
printf("%-7d", i); 
printf("%-14s", tch); 
printf("%-11s", rm);
printf("%-18s", tym);
printf("%-18s", day); 
sum = sum + i;
}
printf("\n-------------------------------------------------------------------------------------------------\n");
printf("There are total of %d subjects.\n", ctr);
printf("There are total of %d units.\n", sum);
File.close();
getche();
return 0;
}

Recommended Answers

All 5 Replies

You need another loop. Are the student loads separated by + symbol as shown?

Delete line 53 -- do NOT close the file here.

ifstream& getload(ifstream& File)
{
   // read one student load
   return File;
}

int main()
{
   ifstream File("filename.txt");
   while( getload(File) )
   {
      char x[3]; // read the line with the + symbol
      File.getline(x,sizeof(x));
   }
}

still got an error.. :expected constructor,destructor error and getload undeclared.. i'm just a bit new in c++.. I'm using DevC++ Compiler..

post your program, and don't forget to put it in code tags

[code]

// your code goes here

[/code]

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

ifstream &getload(ifstream& File)
{
   printf("\n-------------------------------------------------------------------------------------------------\n");
   printf("%-12s%-20s%-7s%-14s%-11s%-18s%-18s", sb,ds,un,tc,room,tm,dy);
   printf("\n-------------------------------------------------------------------------------------------------\n");       

   for(ctr = 0; ctr<5;ctr++)
   {
   File.getline(subj,20,',');
   File.getline(desc,32,',');
   File.getline(unit,5,',');
   int i = atoi(unit);
   File.getline(tch,30,',');
   File.getline(rm,20,','); 
   File.getline(tym,20,',');
   File.getline(day,15,',');
   printf("%-13s", subj);
   printf("%-20s", desc);
   printf("%-7d", i); 
   printf("%-14s", tch); 
   printf("%-11s", rm);
   printf("%-18s", tym);
   printf("%-18s", day); 
   sum = sum + i;
   }
   printf("\n-------------------------------------------------------------------------------------------------\n");
   printf("There are total of %d subjects.\n", ctr);
   printf("There are total of %d units.\n", sum);

   return File;
}


int main()
{
using std::atoi;
std::ifstream File("NEW.txt");
int ctr;
static char subj[150];
int sum = 0;
char desc[150];
char unit[150];
char tch[150];
char rm[150];
char tym[150];
char day[150];
char sb[15] = "Subject";
char ds[12] = "Description";
char un[6] = "Units";
char tc[8] = "Teacher";
char room[5] = "Room";
char tm[5] = "Time";
char dy[4] = "Day";
char x[3];

   while( getload(File) )
   {
      File.getline(x,sizeof("+")); 
   }
   
getche();
return 0;}

You have to move all those variable declarations from main() to getline() function.

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.