fgets/puts creates weird output

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 4
Reputation: anne_1000 is an unknown quantity at this point 
Solved Threads: 0
anne_1000 anne_1000 is offline Offline
Newbie Poster

fgets/puts creates weird output

 
0
  #1
Jul 16th, 2009
Dear all,

I have a weird problem reading a file using fgets. The code below should read the first line of the file "F:\\filename.csv". It works for all sorts of files that I create myself, but it does NOT work for certain files that I download from the internet.

More precisely, I download a csv file from Google Insight (see http://www.google.com/insights/search/#q=London&cmpt=q), and when I try the code on the downloaded file, I don't get the first line but the string has the following value: "ÿþW", which displays as a little square, followed by a capital "W".

Any ideas what's going wrong?
This is the code that I'm using:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ()
  5. {
  6. FILE *in=fopen("F:\\filename.csv","r");
  7. char mystring [100];
  8. fgets (mystring , 100 , in);
  9. puts (mystring);
  10. fclose (in);
  11. return 0;
  12. }

I appreciate any help!
Thanks a lot,
Anne
Last edited by anne_1000; Jul 16th, 2009 at 5:56 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: fgets/puts creates weird output

 
0
  #2
Jul 16th, 2009
Welcome anne_1000,
Google csv is unicode text file.
Last edited by adatapost; Jul 16th, 2009 at 7:39 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: anne_1000 is an unknown quantity at this point 
Solved Threads: 0
anne_1000 anne_1000 is offline Offline
Newbie Poster

Re: fgets/puts creates weird output

 
0
  #3
Jul 16th, 2009
Hi adatapost!
Thanks a lot for the useful hint. Do you know of any straightforward way of either converting theGoogle csv file to ASCII, or reading a Unicode file in C/C++? I tried googleing how to do that, but the answers seem to be extremely complicated...

Thnx,
Anne
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: fgets/puts creates weird output

 
0
  #4
Jul 16th, 2009
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: anne_1000 is an unknown quantity at this point 
Solved Threads: 0
anne_1000 anne_1000 is offline Offline
Newbie Poster

Re: fgets/puts creates weird output

 
0
  #5
Jul 16th, 2009
Hi adatapost,
thanks again. I tried using wstring, but the following code does not even compile, and I can't quite figure out why.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. ifstream myfile("F:\\filename.txt");
  10. wstring line;
  11.  
  12. if (myfile.is_open())
  13. {
  14. while (! myfile.eof() )
  15. {
  16. getline(myfile,line);
  17. wcout << line << endl;
  18. }
  19.  
  20. myfile.close();
  21. }
  22.  
  23. return 0;
  24. }

Any ideas what's going wrong?
The first error message referes to line 16; it says

error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ifstream'
1> c:\program files\microsoft visual studio 8\vc\include\string(528) : see declaration of 'std::getline'
But this doesn't help me in any ways..
Sorry I might be being stupid but I have just started using C/C++.

Thanks,
Anne
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: fgets/puts creates weird output

 
0
  #6
Jul 16th, 2009
You might want to read this thread -- lots of good info about unicode files and how to read them.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: anne_1000 is an unknown quantity at this point 
Solved Threads: 0
anne_1000 anne_1000 is offline Offline
Newbie Poster

Re: fgets/puts creates weird output

 
1
  #7
Jul 16th, 2009
Thanks a lot. I have been trying all sorts of code from the internet to convert unicode strings to ascii strings, but so far nothing works. Most of the stuff that I find online is pretty long, and impossible to understand for me as a beginner.

The following code should do it, but I get an error when compiling.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. wchar_t WideStr[] = L"Unicode!";
  10. char x[] = "longlongstring";
  11. wcstombs(&x, WideStr, 1024);
  12. return 0;
  13. }

The error is
error C2664: 'wcstombs' : cannot convert parameter 1 from 'char (*__w64 )[15]' to 'char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>myCOnverter.cpp
Could someone tell me what that means, or how I can find out myself what the error is?
Thanks,
Anne
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: fgets/puts creates weird output

 
0
  #8
Jul 16th, 2009
wcstombs - convert unicode to multibytes.
  1. wcstombs(x, WideStr, 1024);
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,669
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 123
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: fgets/puts creates weird output

 
1
  #9
Jul 16th, 2009
where did the "C++" forum go?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 139
Reputation: Dream2code is an unknown quantity at this point 
Solved Threads: 11
Dream2code's Avatar
Dream2code Dream2code is offline Offline
Junior Poster

Re: fgets/puts creates weird output

 
0
  #10
Jul 16th, 2009
if u can do some manual work then
1)Open your Unicode file with notepad and save as ANSI.
Then read that file it using C
or else
in DOS use
command:type filename > filename1
Then read the filename1 using C

-----------

If your want to develop a Unicode reading tool using C then
You can go by Ancient Dragon or adatapost.

Thanks,
DP
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 530 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC