Thread: symbols returned when reading file

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    19

    symbols returned when reading file

    Hi, I am currently making a program which has to read a certain line from a file, this is no problem, but the string returned from fgets() contains two strange symbols at the end of the word, a music note and a circle, don't know why these are there, nothing like that is present in the textfile

    here's a summary of the code I'm using:

    Code:
              FILE * pFile;
              int aantalLijnen = 0;
              int randomLijn = 1;
              char opgave[25];
    
              pFile = fopen ("hangman.txt" , "rb");
              if (pFile==NULL)
              {
                //error handling
              }
                       
              while( fgets(opgave,sizeof(opgave),pFile) != NULL)
                aantalLijnen++;
              
              if(aantalLijnen == 0)
              {
                //error handling
              }
                                  
              randomLijn = rand() % (aantalLijnen+1);
              
              rewind(pFile);
              
              fgets(opgave,sizeof(opgave),pFile);
    
              
              fclose(pFile);
              
              printTextScreen(10,80,opgave,RGB(255,255,255));
              flipScreen();
    don't mind the printTextScreen() and flipScreen(), they are just functions of my graphics library

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > contains two strange symbols at the end of the word, a music note and a circle,
    Probably the \r\n at the end of the line, as returned by fgets()

    > pFile = fopen ("hangman.txt" , "rb");
    Use "r" mode only for text files, so that each line returned by fgets() has only a \n (that should get rid of one of the symbols you see).
    How to remove the \n from the buffer returned by fgets() is in the FAQ
    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.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    indeed, the \r and \n where read, with the new file read statement, the \r was the only thing that had to be removed from the string, which wasn't very hard

    thnx for pointing this out to me

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Quote Originally Posted by LowLife

    don't mind the printTextScreen() and flipScreen(), they are just functions of my graphics library
    lol, i havethose exact same ones...

    Are you programming for the PSP? Cause those are in the graphics library pysicology or w/e made....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM