Thread: Characters. Funny looking ones

  1. #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;
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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

  3. #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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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

  5. #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

  6. #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

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  8. #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?

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Maybe this:

    if (!f)
    cout << "Error opening file";
    else
    {
    for (int a = 0; a < 3; a++) {

    f >> getline(Name[a], 15);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Funny characters being printed
    By learninC in forum C Programming
    Replies: 8
    Last Post: 03-17-2005, 09:02 AM
  4. Funny looking characters
    By Wiz_Nil in forum C++ Programming
    Replies: 12
    Last Post: 03-20-2002, 01:23 AM
  5. For some reason, it prints funny characters ???
    By Nutshell in forum C Programming
    Replies: 8
    Last Post: 01-14-2002, 04:27 PM