C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-22-2002, 10:10 AM   #1
Registered User
 
Join Date: Feb 2002
Posts: 27
Characters. Funny looking ones

Hi everyone! I hope that u will be able to help me with this small problem.

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   Reply With Quote
Old 02-22-2002, 10:19 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
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   Reply With Quote
Old 02-22-2002, 10:27 AM   #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   Reply With Quote
Old 02-22-2002, 10:30 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
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   Reply With Quote
Old 02-22-2002, 10:53 AM   #5
Registered User
 
minime6696's Avatar
 
Join Date: Aug 2001
Posts: 267
Post Noticed....

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   Reply With Quote
Old 02-25-2002, 08:08 AM   #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   Reply With Quote
Old 02-25-2002, 08:14 AM   #7
Confused
 
Magos's Avatar
 
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   Reply With Quote
Old 02-25-2002, 08:31 AM   #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   Reply With Quote
Old 02-25-2002, 12:33 PM   #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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:36 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22