![]() |
| | #1 |
| Registered User Join Date: Feb 2002
Posts: 27
| Characters. Funny looking ones ![]() I got this program that reads characters from a txt file and stores them in a array. I then want to display the characters but when i do it comes up as squares and funny looking symbols? Why is this, what is wrong with my code? Your help is greatly appreciated ![]() Many thanks in advance ![]() #include <iostream.h> #include <fstream.h> int main() { char Name[3][15]; fstream f("a:Names1.dat", ios::in); if (!f) cout << "Error opening file"; else { for (int a = 0; a < 3; a++) { for (int b = 0; b < 15; b++) { f >> Name[a][b]; } cout << endl; } } f.close(); for (int c = 0; c < 15; c++) { for (int d = 0; d < 15; d++) { cout << Name[c][d]; } cout << endl; } } |
| Wiz_Nil is offline | |
| | #2 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,639
| This is your input loop for (int a = 0; a < 3; a++) { for (int b = 0; b < 15; b++) { This is your output loop for (int c = 0; c < 15; c++) { for (int d = 0; d < 15; d++) { Me-thinks one of those 15's should be 3 |
| Salem is offline | |
| | #3 |
| Registered User Join Date: Feb 2002
Posts: 27
| Still funny characters Yeh u were right about one of the variable being 3, c was suppose to be 3. I've changed it but I am still getting funny symbols. Any ideal why is happening??? Pleaseeee |
| Wiz_Nil is offline | |
| | #4 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,639
| Do you have 45 chars in your file? > f >> Name[a][b]; better check for f.error() being true, or perhaps f.eof() > char Name[3][15]; Initialise it to 0, or perhaps fill it with '@' chars before trying to read the file. If it's still got '@' when you print it, then you know something's wrong |
| Salem is offline | |
| | #5 |
| Registered User Join Date: Aug 2001
Posts: 267
| Well, I noticed that the filename is "a:Names.dat", two thingz, 1.) Thats not a valid filename under win32 platform, are u sure u didnt mean "a:\\Names.dat"? (\\ = escape sequent for '\'). 2.) U should post the contents of the file so we can see. "Crazy Characters" are usually becouse the loading of the file failed, and thats just "garbage" left over in the buffer, just look into the filename thing and stuff...cuz thats prolly the problem! SPH |
| minime6696 is offline | |
| | #6 |
| Registered User Join Date: Feb 2002
Posts: 27
| I'm putting aside the problem above and working on a similar problem that is related to the above problem slightly. #include <iostream.h> #include <fstream.h> #include <conio.h> #include <ctype.h> int main() { char Name[3][15]; fstream f("a:abc.dat", ios::in); if (!f) cout << "Error opening file"; else { for (int a = 0; a < 3; a++) { for (int b = 0; b < 15; b++) { f >> getline(Name[a][b], 24, ' '); } cout << endl; } } f.close(); } What I'm trying to do is read characters from a file and store them into an array. There is something wrong with my code though, it doesn't work. Can you help, is it an syntax or is the code just screwed up !!!!Your help is appreciated |
| Wiz_Nil is offline | |
| | #7 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,124
| Are you sure its a txt file you're opening? Most of the time when those squares and "funny characters" show up is when you open a binary file as text.
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #8 |
| Registered User Join Date: Feb 2002
Posts: 27
| I think it is as a text file. I'm pretty sure it is. And the file that it is reading from has the extension .dat but is save as a text file in notepad. Is this OK? |
| Wiz_Nil is offline | |
| | #9 |
| Registered User Join Date: Oct 2001
Posts: 2,936
| Maybe this: if (!f) cout << "Error opening file"; else { for (int a = 0; a < 3; a++) { f >> getline(Name[a], 15); } |
| swoopy is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A development process | Noir | C Programming | 32 | 12-19-2009 10:15 AM |
| [URGENT] Getting warning: null character(s) ignored repeatedly | headbr | C Programming | 10 | 07-10-2008 03:45 PM |
| Funny characters being printed | learninC | C Programming | 8 | 03-17-2005 09:02 AM |
| Funny looking characters | Wiz_Nil | C++ Programming | 12 | 03-20-2002 01:23 AM |
| For some reason, it prints funny characters ??? | Nutshell | C Programming | 8 | 01-14-2002 04:27 PM |