Thread: Need help regarding file reading

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Need help regarding file reading

    I'm having trouble reading from file and saving the data into integer / double variables.

    These are the contents of the file I have to read from.

    & Student Roll Number, Student Name, CGPA
    09i-0947, Peter Clark, 2.01
    09i-01716, James Morrison, 2.02
    10i-0024, William Craft, 2.09
    & End

    The code should ignore all lines starting with an & sign.
    Then I have to save the starting 2 numbers in an integer variable called batch. The numbers after the "-" sign in an integer called rollnumber. Then the name and then the CGPA in a double variable. It should ignore the commas and the spaces. This is what I've come up with as yet but I don't know what to do next.

    Code:
    int x=0, y=0;
    	bool status;
    	ifstream fin;
    	fin.open("input.txt");
    	int i[100];
    	char getline[200];
    	char skip;
    	char batch[200][5], roll[200][6], name[200][30], cgpa[200][5];
    	char batcha[5], rolla[6], namea[30], cgpaa[5];
    	x=fin.tellg();
    	do
    	{
    		int count=0;
    		fin>>skip;
    		fin.seekg(x);
    		if(skip=='&')
    		{
    			fin.ignore(100, '\n');
    			status=false;
    		}
    		else
    		{
    			y=fin.tellg();
    			fin.getline(getline,200, '\n');
    			cout<<getline<<endl;
    			status = true;
    		}
    		x=fin.tellg();
    		if(status==true)
    		{
    			fin.seekg(y);
    			fin.getline(roll[count], 5, ',');
    			fin.getline(name[count], 30, ',');
    			fin.getline(cgpa[count], 5, '\n');
    			count++;
    		}
    	}
    	while(!fin.eof());
    I have tried various other methods like string tokenizing etc but failed.

    Regards.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    You're nearly there I think.

    Code:
        char batch[200][5], roll[200][6], name[200][30], cgpa[200][5];
        char batcha[5], rolla[6], namea[30], cgpaa[5];
    I'm not sure what this is all about: from the code you have, I would say you need to change the type of batch, roll, cgpa to be what you said in your post -- i.e. int batch[200] etc. If you're going to use getline(), you'll need 1 or several char arrays to read in to, you could keep "batcha" and friends for this.

    You're on the right track with getlines. I think you need another line to get "batch".

    The main problems with the code are:
    - your arrays are too small. Remember that the strings read in are NULL terminated, so for example "01716" is 6 characters. This can lead to horrible hard to debug behaviour. Likewise your getline limits are too small by 1 character.
    - I'm not sure what you're trying to do with x and y? Something to do with getting back to the start of the line you're currently parsing, I think. Can you give the variables meaningful names? I think you'll see that you just need one of them.

    Doing it this way you'll end up with, I hope, 4 temporary strings with the data you want in them. All that remains is to convert them to the appropriate data type. Plenty of guidance on that around the place already.

    Hope that helps you get on the right track. I'm not sure this is the best way to approach the problem, but I can't think of a better one (other than "don't use C++ ) off the top of my head.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. File I/O..Reading a file and printing contents
    By eeengenious in forum C Programming
    Replies: 2
    Last Post: 03-14-2011, 05:58 PM
  3. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  4. Reading in a binary file from offset into another file
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2006, 03:01 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM