5 Replies - 411 Views - Last Post: 01 December 2014 - 06:21 AM Rate Topic: -----

#1 rickyhitman10   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-November 14

mistake opens file in c++

Posted 30 November 2014 - 09:23 PM

i have contents data in txt file, and include my data to array 2D... there is mistake from my declaration in line opf.open (filename), how to fix it, i am using IDE Netbeans. anyone can help me, thanks

#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <stdio.h>
using namespace std;

int row,col;
int main(int argc, char** argv) {
    //declaration...
    string str;
    string filename;
    int x;
    int arr [25][25] = {{0}};
    ifstream opf;
    //--------------------------------
    //enter the data from keyboard
    cout<<"enter the data in below, ex (:c:/data.txt) :\n";cin>>filename;
    opf.open(filename); //error?????????
    
    //check data....
    if (opf.fail()){
        cout<<"the file you are access cannot found\n";
        exit (1);
    }
    while (opf.good()){
        while (getline(opf,str)){
            istringstream streams (str);
            col =0;
            while (streams>>x){
                arr[col][row] = x;
                col++;
            }
            row++;
        }
    }//displaying size data rows and columns...
    cout<<"size of rows : "<<row<<endl;
    cout<<"size of rows : "<<col<<endl;
    
    //reading data file...
    for (int i=0; i<row; i++){
        for (int j=0; j<col; j++){
            cout<<left<<setw(6)<<arr[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

This post has been edited by JackOfAllTrades: 01 December 2014 - 05:12 AM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: mistake opens file in c++

#2 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: mistake opens file in c++

Posted 01 December 2014 - 02:21 AM

Please put your code lines between the code tags when you are posting on the forums :)
Also, what is the error you are getting? Is it a compile error or a logic error?

There is one thing off my head, one of the most common errors when trying to input text from user is when you use getline after you have used cin>> (though I am not sure if you did this). The problem is that when you use cin>>, it only discards the leading whitespaces, does formatted input, and the trailing whitespace characters (usually, a newline character) still remain un-extracted. Lets see an example:
#include <iostream>
#include <string>

using namespace std;

int main()
{
   string name;
   cout<<"Please enter your name:"<<endl;
   cin>>name;
   cout<<"Now write a message:"<<endl;
   string message;
   getline(cin, message);
   cout<<"message received!"<<endl;
   cout<<"here's your message: "<<message<<endl;
   return 0;
}

This is the output that I get:

Quote

Please enter your name:
myname (I press enter here!)
Now write a message:
message received!
here's your message:

The problem: weird! The program doesn't even let me write my message! That's because cin>> left the newline character un-extracted from the stream. So when I call getline, this newline character is mistakenly read as my input which is not what I wanted! getline reads the newline character and because it is a delimiter, it stops right here.
What you need to do is to extract and discard the left-over junk before you use getline:
#include <iostream>
#include <string>
#include <limits> //for numeric_limits

using namespace std;

int main()
{
   string name;
   cout<<"Please enter your name:"<<endl;
   cin>>name;
   cout<<"Now write a message:"<<endl;
   string message;
   cin.ignore(numeric_limits<streamsize>::max(), '\n'); //extract and discard characters
   getline(cin, message);
   cout<<"message received!"<<endl;
   cout<<"here's your message: "<<message<<endl;
   return 0;
}

cin.ignore reads and extracts characters until one of these happen:
  • eof is reached.
  • the count of characters read and discarded reaches the specified count in the first parameter (usually, it's numeric_limits<streamsize>::max().
  • the delimiting character specified in the second argument is reached. In this case, the delimiting character itself is discarded as well.
.
You can read more on this function here.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: mistake opens file in c++

Posted 01 December 2014 - 05:18 AM

According to the documentation on ifstream's open() function, the filename should be a const char *, not a string. You can get a const char * from a string by using the c_str() method of the string class.
Was This Post Helpful? 0
  • +
  • -

#4 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: mistake opens file in c++

Posted 01 December 2014 - 05:26 AM

View PostJackOfAllTrades, on 01 December 2014 - 03:48 PM, said:

According to the documentation on ifstream's open() function, the filename should be a const char *, not a string. You can get a const char * from a string by using the c_str() method of the string class.

That's true. But as a little side note, from C++11 onward, there is an overloaded version which takes a string. It's also stated in the same link, just switch to the C++11 tab.
Was This Post Helpful? 1
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: mistake opens file in c++

Posted 01 December 2014 - 05:39 AM

I'm OLD...there's no such thing as C++11 ;)
Was This Post Helpful? 0
  • +
  • -

#6 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Re: mistake opens file in c++

Posted 01 December 2014 - 06:21 AM

View PostJackOfAllTrades, on 01 December 2014 - 04:09 PM, said:

I'm OLD...there's no such thing as C++11 ;)

Come on!!! Don't say that Jack :whatsthat: You are young, with an extra patch of experience updates :wink:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1