0

So im trying to read an .xls file using ifstream and the executable return an uninteligible string.

My function is as follows

void submenuP(){
 ifstream ifile;
 ifile.open("C:\\Users\\VDSCH\\Desktop\\Productos\\Peluches\\Modelos.xls", ios::in);
    ifile.exceptions(ifstream::badbit | ifstream::failbit);

    string s;
    int c;

system("CLS");
if (!ifile){
    cerr<<"file not found"<<endl;
      exit(1);

}


cout<<"\n\n\t Lista de Peluches\n";
while(!(ifile>>ws).eof()){
    ifile>>s;
    if(ifile.fail()) exit(1);
    cout<<s<<endl;
}

ifile.close();
}

the function is supposed to only display the contents of the file but it returns this :

     Lista de Peluches
╨╧αí▒

Is there something wrong with my function or perhaps is the compiler , im using g++ in mingw for windows and have recently updated the libraries

Thanks in advance

VicSan
  • 119
  • 1
  • 9
  • 1
    An `.xls` file is a binary file not text. Open it in notepad and see for yourself. – drescherjm Nov 20 '18 at 17:49
  • `while(!(ifile>>ws).eof()){` is wrong. – drescherjm Nov 20 '18 at 17:50
  • 1
    What did you _expect_ the contents of a .xls file to look like? You can export your spreadsheet as a .csv file if you want the contents as (unformatted) text – Useless Nov 20 '18 at 17:56
  • but i have a function that reads a .xls file and it displays the correct information to the screen the only difference being that the file is in the same directory – VicSan Nov 20 '18 at 18:27
  • also i want to print a list found in a .xls file – VicSan Nov 20 '18 at 18:30
  • If you have code that reads a `.xls` file use that. It's not a text file so you can't read it the way you are trying. – drescherjm Nov 20 '18 at 18:35
  • well , like i said the code above reads a .xls file just fine in the same directory but it prints out the above output when i try to read from a diferent directory – VicSan Nov 20 '18 at 18:41
  • The directory should not make a single difference. Have you looked at the file with notepad yet? – drescherjm Nov 20 '18 at 18:42
  • i opened it in notepad++ and it appears as a bunch of NUL, SO , ENQ and such – VicSan Nov 20 '18 at 18:47
  • 1
    So you can't read it as text because it is not text. Do what @Useless mentioned in his comment. – drescherjm Nov 20 '18 at 18:49
  • I can read the file now but still outputs this before the text : '∩╗┐' – VicSan Nov 20 '18 at 19:45
  • @Victor Daniel Santilln Chalico, the .xls file in the same directory that your code is able to read, can you open that file in a text editor and see if it's actually a .CSV file with .xls extension? – iVoid Nov 20 '18 at 19:52
  • ***I can read the file now but still outputs this before the text*** Because it is not text. A binary file can have embedded readable text but also garbage characters. Again unless you are writing an excel reading library you can not do what you want. An excel reading library would take several thousand lines of `c++` code. The other file must have been a text file with the wrong extension. – drescherjm Nov 20 '18 at 19:57
  • You may want to use this if you can't just convert the file to csv in excel: http://www.libxl.com/ – drescherjm Nov 20 '18 at 20:07
  • https://www.quora.com/How-do-I-parse-Excel-files-in-C++ – drescherjm Nov 20 '18 at 20:09
  • I created a new CSV file with the same data and it still outputs the same characters – VicSan Nov 20 '18 at 20:38
  • Use a proper csv parser. https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c – drescherjm Nov 20 '18 at 20:46

0 Answers0