GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Nov-2005, 04:21
rajusb rajusb is offline
New Member
 
Join Date: Nov 2005
Posts: 29
rajusb is on a distinguished road

help


{
int m[25][3];
int i=0;
int j=0;
ifstream fin;
fin.open("try1.txt");
if (fin.fail())
{
cout << "ERROR opening file.";
}
else
{
while (!fin.eof())
{
fin >> m[j]; Error in this line
j++;
}
}
for (int k=0;k<25;k++)
{for(int j=0;j<3;j++)
{
cout << m[k][j] << "\t";
}
cout << endl;
}

} the error is "no operator defined which takes a left-hand operand of type 'class ifstream' (or there is no acceptable conversion)
Error executing "
  #2  
Old 23-Nov-2005, 07:39
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: help


Hi rajusb,

Please read the Guidelines.
If you insert your code between [c++] and [/c++] tags, that would be very useful for us.
and please dont double post.
Thank You.

You have made good progress in this program.
But, the error came because you declared m as:
CPP / C++ / C Code:
int m[25][3];
and used it like a single dimensional array like this:
CPP / C++ / C Code:
fin >> m[j]; //there IS an error in this line.

So, what you should do is to do something like this:
CPP / C++ / C Code:
//...
fin >> m[j][k];
//...


But using operator >> always result in some scanning error.

So, we can get a whole line from the file at a time, then scan the files from the line.

Consider that you have a file say try1.txt.
The contents is:
Code:
1 1 28.3 29.3 19.7 1 2 45.3 23.7 -90.6 1 3 17.8 -12.7 -14.5
Note that there should not be extra spaces inbetween.

Here is a sample C++ program to read from the file and print it:
CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
    char s[30];                 //string to read a line from the file
    int first[10];              //integer arrays to get integers from file
    int second[10];             
    float third[10];            //float arrays go get floatint point numbers from file
    float fourth[10];
    float fifth[10];
    int no_of_lines, j;         //no_of_lines holds the number of lines read from the file
    
    ifstream fin;               //create new inputstream and open file try1.txt
    fin.open("try1.txt");
    
    if (fin.fail())             //check whether there is any error in opening the file
    {
        cout << "ERROR opening file.";
    }
    
    else                        //if there is no error message
    {
        j = 0;                  //initialize counter to 0
            
        while (!fin.eof())      //check whether the end of file is reached.
        {
            fin.getline(s, 30); //get a line from the file
            
            //scan the string s for the integers and floats.
            sscanf(s, "%d %d %f %f %f ", &first[j], &second[j], &third[j], &fourth[j], &fifth[j]);
            
            j++;                //increment the counter j
        }
    }

    no_of_lines = j;            //number of lines is j
    
    //print the results.
    cout<<"The input from the file is.. "<<endl;    
    for( j=0; j < no_of_lines ;j++)
    {
        cout<<first[j]<<" "<<second[j]<<" "<<third[j]<<" "<<fourth[j]<<" "<<fifth[j]<<endl;
    }
    
    cout << endl;
    
    getchar();
    getchar();
    return 0;
    
}


Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
Last edited by Paramesh : 23-Nov-2005 at 08:55.
  #3  
Old 24-Nov-2005, 03:34
rajusb rajusb is offline
New Member
 
Join Date: Nov 2005
Posts: 29
rajusb is on a distinguished road

Re: help


Hi,
Thanks a lot.
sorry i wanted to write comments! I dont know why i am not getting the output. i am not getting error also.after this it should prit the textfile right but i am not getting that!

hope i am not bothering u much. I am writting this in vc++
  #4  
Old 24-Nov-2005, 03:38
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: help


Hi rajusb,

Did you create and save the file try1.txt as:
Code:
1 1 28.3 29.3 19.7 1 2 45.3 23.7 -90.6 1 3 17.8 -12.7 -14.5

Then it will print correctly.
Just copy and paste this content in notepad and save it as try1.

(what a coincidence that we both are online in the same time!)

Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #5  
Old 24-Nov-2005, 03:59
rajusb rajusb is offline
New Member
 
Join Date: Nov 2005
Posts: 29
rajusb is on a distinguished road

Re: help


Ya, we r online on the same time. I have saved in my file but i am not getting the output?
  #6  
Old 24-Nov-2005, 04:00
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: help


Quote:
Originally Posted by rajusb
Ya, we r online on the same time. I have saved in my file but i am not getting the output?
Did you save the file in the place where you have the C++ program executable?
Did you get any error message?
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #7  
Old 24-Nov-2005, 04:01
rajusb rajusb is offline
New Member
 
Join Date: Nov 2005
Posts: 29
rajusb is on a distinguished road

Re: help


ya in my file. I am not getting any error message
  #8  
Old 24-Nov-2005, 04:04
rajusb rajusb is offline
New Member
 
Join Date: Nov 2005
Posts: 29
rajusb is on a distinguished road

Re: help


I have written the code in OnLBUTTONDOWN, Its showing the window,I have written in ONDRAW,Press the LBUTTONDOWN but its not displaying anything
  #9  
Old 24-Nov-2005, 04:09
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: help


Quote:
Originally Posted by rajusb
I have written the code in OnLBUTTONDOWN, Its showing the window,I have written in ONDRAW,Press the LBUTTONDOWN but its not displaying anything
The problem is that:
The code i showed you is a console program.
Not a windows program.

That was a sample program.
Just to give you some information about how to read from a text file.

If you want to write a windows program, Just remove the cout statements and then use the arrays to display in a windows component(may be try a textbox to display one value first.)

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #10  
Old 24-Nov-2005, 04:15
rajusb rajusb is offline
New Member
 
Join Date: Nov 2005
Posts: 29
rajusb is on a distinguished road

Re: help


ya ok let me read a first row and first column for that if use array then to print please can u do for me to read first row and first column please!!!!

11 23 4.56 3.54 9.56 45 56 34 65 67 9.65 9.89 8.65
23
3.56
67
76
87
6.78
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:16.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.