Thread: null after every character!!?!

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    null after every character!!?!

    Hello all!

    I need a little help if someone has the patience.
    At the moment i'm using Dev-c++ with windows XP however i'm experiencing
    some odities which are a little ahead of my understanding.
    you see i was writing a small program to overwrite files before i erase them
    however whilst doing this i found that the fgets function was not reading the lines
    of txt from the file correctly and that previous projects that had worked fine were
    also going wrong. When i printf the contents of my array i find that each character
    is followed my a null.....every single letter!!

    heres my simple little code which gives the results described:

    Code:
       #include<stdio.h>
    
    main()
    {
        int i;
        char trial[50];
        FILE *ptr;
        ptr=fopen("c:\\at.txt","r");
        fgets(trial,200,ptr);
        printf("%s\n",trial);
        for (i=0;i<50;i++)
        {printf("%i,",trial[i]);}
        fclose(ptr);
        getchar();
        return 0;
    }
    if its of any importance in was using notepad to write my txt files.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Have you verified the file is open, i.e. check the pointer returned by the fopen call?

    Quote Originally Posted by the bassinvader
    Code:
    char trial[50];
    
    ...
    
    fgets(trial,200,ptr);
    Why are you reading 200 characters worth of data into an array that can only hold 50 characters?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    For one thing, this is bad:
    Code:
    char trial[50];
    ...
    fgets(trial,200,ptr);
    You're telling fgets to read up to 199 characters, but your array is only 50 elements big.

    EDIT: beaten again!
    If you understand what you're doing, you're not learning anything.

  4. #4
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    Just an idea, but if the file is unicode encoded something like that might happen.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I'm not exactly sure what you're trying to do, but I'm positive printf starts failing because it expects an integer, yet you passed in characters.

    > fgets(trial,200,ptr);
    fgets can be as bad as gets if you are not honest about your string sizes.

    Try something like this, perhaps:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main ( void )
    {
       int i;
       char trial[50];
       const char filename[] = "C:/at.txt";
       FILE *ptr = fopen(filename, "r");
       if( ptr != NULL )
       {
          fgets(trial, sizeof trial, ptr);
          for( i = 0; i < sizeof trial; ++i )
          {
             putchar( isprint(trial[i]) ? trial[i] : '.' );
          }
        fclose(ptr);
       }
       else
       {
         perror(filename);
       }
       return 0;
    }
    $ ./foo
    This is a sample...a......................"..5.a.v

    It's kind of goofy if you insist on printing the whole buffer, and not just what you can read.
    Last edited by whiteflags; 09-08-2006 at 10:54 AM. Reason: pokey...

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'm positive printf starts failing because it expects an integer, yet you passed in characters.
    Character, integer...same difference. Characters just have a smaller range.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by SirNot
    Just an idea, but if the file is unicode encoded something like that might happen.
    I second this. My first thought was "Hey, must be a unicode text file."
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If it is a unicode file, then ordinary fgets() isn't going to read past the first nul (as far as I know).

    > char trial[50];
    Try
    memset( trial, '@', sizeof trial );
    fgets( trial, sizeof trial, fp );

    At least then you'll have a reasonable chance of seeing how much data you really read from the file, and how much is just "garbage" in your otherwise uninitialised char array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Smile

    wow !!!
    i really should've checked for replies earlier!!

    Ok. U're right about the fgets this is just a little accident cause i pasted from a bigger program. This was just a quick thing i put together in order to test to see what was going on, so the program has no purpose other than that, the original code was just to big to
    reproduce here.

    The file does open fine, ....pasted.

    If you look at the code i originally try to print the result of the fgets as a string, then i printf
    each individual character as an integer. This is how i know that each character is followed
    by a null.

    Finally i must admit total ignorance of you're referals to unicode so could someone send me to a place to educate myself?

    Thanx for your help.....and continued help

  10. #10
    -AppearingOnThis..........
    Join Date
    May 2005
    Location
    Netherlands
    Posts
    44
    If it is a unicode file, then ordinary fgets() isn't going to read past the first nul (as far as I know).
    Well, according to opengroup:
    The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a <newline> is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte.
    Finally i must admit total ignorance of you're referals to unicode so could someone send me to a place to educate myself?
    Wikipedia probably isn't a bad place to start: http://en.wikipedia.org/wiki/Unicode

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Salem
    If it is a unicode file, then ordinary fgets() isn't going to read past the first nul (as far as I know).
    Why not? You're telling fgets how much to read, not to look for a null delimiter. fgets stops reading on a new line, EOF, or when it hits count - 1.
    Quote Originally Posted by the bassinvader
    Finally i must admit total ignorance of you're referals to unicode so could someone send me to a place to educate myself?
    I could have sworn I already posted a link.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  12. #12
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    Yeah you did post a link sorry.

    In my haste to reply i overlooked it. Thanx

    But i gotta admit that i feel a little bogged down in things that i dont really understand.
    I'm just a learner so i just need a little help understanding why the program doesnt do what i was expecting, i mean its not exactly difficult code is it!! And why do programs that previously worked fine now go mad?

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Who knows? This is programming, not cooking.

    I have programmed for over a year and I have not learned much about Unicode yet. There isn't any real need at this point. If you're writing internationalized applications et al, then you have a good reason to learn non-ASCII encodings. For now, you should be working on programming basics.

    You should stop writing programs which look like IOCCC entries -- space them out, structure them a little. Check out Salem's avatar too.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  14. #14
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by the bassinvader
    Yeah you did post a link sorry.

    In my haste to reply i overlooked it. Thanx

    But i gotta admit that i feel a little bogged down in things that i dont really understand.
    I'm just a learner so i just need a little help understanding why the program doesnt do what i was expecting, i mean its not exactly difficult code is it!! And why do programs that previously worked fine now go mad?
    It definately sounds like you saved the text in Unicode (specifically UTF-16). It uses 16 bits (2 bytes) for each letter, and for any ASCII character, the high-order byte is zero. You can always open notepad and see how it's saving them (ANSI or Unicode) in the file->save options.

    Get a good hex editor and take a look at the data file itself -- that should tell you if the problem is with your reading of the file or the underlying file itself.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  15. #15
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Wink

    thanx cat....

    finally something i can understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM