reading from file and writing in a nother file

This is a discussion on reading from file and writing in a nother file within the C++ Programming forums, part of the General Programming Boards category; I want to make aprogram to read a file including the marks of student where each line includes the student ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    127

    reading from file and writing in a nother file

    I want to make aprogram to read a file including the marks of student where each line includes the student name ,his bench number and mark .. i want to use this program to write that in a nother file which will include the same things included in the input file and also to calculate the average of the marks of the student and write it in the output file

    i wrote the following and it doesnt work can you tell me what are the problems with my code
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<fstream>
    using namespace std;
    
    struct student
    {
    char name[];   // for the name of the student
    int BN;             // for the bench number
    int mark; //for the mark of the student
    
    
    int main()
    {
    	student m;
    	double sum ,av; // sum is the sum of the marks of students and av is their average
    	int i=0;  //counting the nuber of students
    	sum =0;
        ifstream fin;
        ofstream fout;
        fin.open("E://marks.txt");
        if (fin.fail())
    	{
           cout<<"Can not open file\n";
            exit(1);
    	}
      fout.open("E://results.txt")
      fin>>m.name;
      while(!fin.eof())
      {
    	  fout<<m.name;
    	  fout<<"  ";     //space
    	  fin>>m.BN;
    	  fout<<m.BN;
    	  fout<<"  ";
    	  fin>>m.mark;
    	  sum+=m.mark;
    	  fout<<m.mark;
    	  fout<<endl;
    	  i++;
    	  fin>>m.name;
      }
      av=sum/i;
      fout<<" the average of the students is :"<<av <<endl;
      fin.close();
      fout.close();
      return 0;
    }
    so any one can tell me what are the errors and also if there is another method to do that
    thanks in advance

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    sorry i missed something for the structure

    Code:
    struct student
    {
    char name[];   // for the name of the student
    int BN;             // for the bench number
    double mark; //for the mark of the student
    };
    i hope anyone tell me where is the error in my program

    thanks in advance

  3. #3
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Code:
    fout.open("E://results.txt"); <--- // ye 4got this on my 5 secs of observation.
    What exactly is the problem?? It helps if you say so that we can know what to look for.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  4. #4
    Registered User Ajsan's Avatar
    Join Date
    Dec 2003
    Posts
    55

    i think this should fix some of it

    its treating your forward slashes in the E:// as comments for every thing after the // in that line

    you should be using \\ change all the things that arn't comments from // to \\ then try it again.
    Style is overrated.

    - ₽ -

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Thanks all i eventually detected the error

    in the struct i didnt define the size of the array of name
    i should put for example
    Code:
    char name[20]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21