Reading special characters off a file

This is a discussion on Reading special characters off a file within the C++ Programming forums, part of the General Programming Boards category; Hello to all, kinda new here, but here goes So I was making a multiple choice quiz program, that reads ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    9

    Reading special characters off a file

    Hello to all, kinda new here, but here goes
    So I was making a multiple choice quiz program, that reads multiple choice questions off of a file, but I have encountered a problem.
    While reading " character or ' character or the rest of those special character that are supposed to be greeted by hackslash, I get some kind of a very weird character when the program runs and display the data read.
    So here is my question.
    Is there any way I can use getline or some other command to make sure that when it reads the data it actually displays a correct character instead of some weird unknown square or something else?
    I have tried everything, but to no avail. Any help is appreciated.
    Thanks a lot for your help in advance.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,243
    You need to post your source. The smallest example the demonstrates your problems.

    Soma

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Pretty long but ok

    Code:
    getline(quizData, line);
            
            while(line.compare("qend")!=0)
            {
                string assembler = "";
            
                while(line.compare("end")!=0)
                {
                    assembler.append(line);
                    assembler.append("\n");
                    getline(quizData, line);
                }
                
                question[i][b][0] = assembler;
                
                getline(quizData, line);
                
                question[i][b][1] = line;
                
                b++;
                
                getline(quizData, line);
            }
    Anyways I dunno if this makes any sense
    But the problem is that when I use getline command and the file contains " character then it will not read correctly it and instead display squares and triangles.
    I tried putting hackslash before the quotations marks in hopes that it will fix it, but instead it simply shows hackslash followed by a weird character.
    Is there a way to read character by character and then put it together? I am at my wits end here.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,243
    O_o

    That's not really "The smallest example the demonstrates your problems.".

    Have you verified your data?

    I'm assuming you are using a GUI editor that uses Unicode, in UTF8, to visually distinguish between the "starting" and "ending" quote characters.

    Soma

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Yeah I am using notepad++. What should I use then?
    Or should I input some kind of other character, in hopes it will bug into the correct one?
    And yes you are right unicode is UTF8
    Amazing how you figured it out.
    You had same error before?

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,243
    What should I use then?
    I'm fine with you using whatever you want, but if you want to use the "native", and portable, file handling provided by the C++ standard library you need to save the file as ASCII text.

    You had same error before?
    I've been programming for almost 25 years. I've made virtually every error one can make and still be alive. I also make it a point to learn from the mistakes of others.

    Soma

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    9
    Sorry for a non C++ related question, but how would I save something as a ASCII text?
    I tried notepad and notepad++ and gedit and nothing
    And looked up google too
    Thanks for all your help you gave me so far

  8. #8
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    You can save the file as ascii in the "save as" dialogue. There should be something for the encoding. Make sure it is set as ANSI. I believe ANSI is a single byte encoding so each character only gets one byte, which would follow the ascii chart.
    -- Will you show me how to c++?

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Cannot speak for the others but gedit should work fine.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

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. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

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