ANOTHER File i/o problem (well just "i" really)

This is a discussion on ANOTHER File i/o problem (well just "i" really) within the C++ Programming forums, part of the General Programming Boards category; Hello, another newb with another file i/o problem here. I can output ok, not that much of a problem. The ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    7

    ANOTHER File i/o problem (well just "i" really)

    Hello, another newb with another file i/o problem here.
    I can output ok, not that much of a problem. The only thing is that one of those characters I output to file happens to be an odd one (ASCII code 12, a female symbol (you know, with the circle and cross) to be specific).

    When I then try to input the file to a string, it simply ignores this characrer (it is the first one in the file by the way). I have tried replacing it wuth "normal" characters,(7 and k), and they are accepted.

    Little odd, that is all, hoping someone here can help me out.
    Thanks.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How did that character get in the file in the first place? It's a form feed, and is probably considered to be white space, so cin won't see it (if that's what you're using).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    hmmm, ill show u the relevant code:

    ifstream lmdb("dataen.sdt"); //load file
    cout << "Loading, please wait\n\n";
    //load the file into a string
    lmdb >> tempstring;
    cout << tempstring;

    However, when outputted usign cout, tempstring does not contain the first character in the file "dataen.sdt" The first part of this file by the way, looks like this:

    9Qamrr*Kgjjglerml*Kgjjw*9Hm_l


    hmmm, just noticed using this font the symbol that ussually appears looks like the square above...could be a problem. Is there a different command i can use to bypass that problem?
    Thanks for any advice.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What is the definition of tempstring? Is it a char* ?

    Post some more code so we can see what you're up to.

    [edit]Also, did you check that the file open worked successfully ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    tempstring is a character array :

    char tempstring[1000];

    dunno what other code u really need, but here is the whole load function:
    Code:
    int load()
    {
       char tempstring[1000] = ""; //the data gathered from the file
       // certain key symbols used in the computation
       char semicolon[16] = ";" ;
       char comma[16] = "," ;
       // character number in the tempstring being compared
       int charnum = 0;
       int charnum2 = 0; //end of word
       ifstream lmdb("dataen.sdt"); //load mini database
       cout << "Loading, please wait\n\n";
       //load the file into a string
       lmdb >> tempstring;
          cout << tempstring; /*this is where i can see that tempstring  isn't getting all the info */
    i don't think anything beyond this point is relevant, but....

    Code:
          //unencrypt
       for (int i = 0; i < strlen(tempstring); i++)
            {tempstring[i] = encrypt (1, tempstring[i], enkey);}
       charnum = lookfor (tempstring, 0, semicolon[0]);
             // following is new load system for ndn
       ndn = (int)tempstring[charnum - 1];
       // Now lets get the info
       int count = 0;
       for (int i = 0; i < ndn ; i++)
       {
          dbe1[i].memID = i;
          charnum = lookfor(tempstring, charnum2, semicolon[0]);
          charnum++; //get it to the next character
          charnum2 = lookfor(tempstring, charnum, comma[0]);
          getdata (dbe1[i].firstName, tempstring, charnum, (charnum2 - 1));
          charnum = ++charnum2;
          charnum2 = lookfor(tempstring, charnum, comma[0]);
          getdata (dbe1[i].lastName, tempstring, charnum, (charnum2 - 1));
          charnum = ++charnum2;
          charnum2 = lookfor(tempstring, charnum, comma[0]);
          getdata (dbe1[i].nickName, tempstring, charnum, (charnum2 - 1));
       }
       return 0 ;
    }
    As you can see, very newbie code style, also not sure how to check if load was successful, bit of a newb, sorry . If you have any other suggestions as to how change things for the better i would also appreciate them (intrigued by how char pointers can be used in place of arrays?)
    Hope this helps (helps me really, but anyway)
    Thanks again.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Do this before we go any further:

    (Always check your input streams before using them.
    Code:
    ifstream lmdb("dataen.sdt");
    
    if (!lmdb)
    {
       cout <<"Error opening the file.  program halted." <<endl;
       return / exit / whatever you want
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the code for reading into tempstream from the file looks okay, once you test for the stream being open, but the question remains, how did you write the information to the file? There may well be something lurking there you didn't intend, so when you read out the unintended material the output is not what you expected.

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    I did the check thing (and even added an else statement to be sure), and it definetly is loading correctly. Also, I don't think it has to do with the how i outputted to the file - I've opened the file in notepad and it looks as intended. I think it has to do with the type of character I'm trying to import - The data i gave earlier I've noticed doesn't contain the first character which is the one that isn't being imported properly. I think the comp is deliberately ignoring it, and im wondering if there is a way that i can stop the comp doing that.
    The character is a female symbol (the circle with the cross) and i believe is number 12 on the ASCII... thing.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Ranorith, Hammer's first post is most likely correct. This character is being skipped because it's a form-feed character. You can't count on ANYTHING below space (32 dec) working correctly; these characters have multiple meanings to the computer.

    You can read/write this character in binary mode, but you probably can't even output it with cout to begin with.

    For example, try this:

    cout << (char)12 << "Hi there";

    I would wager that it only prints "Hi there", because it interprets this as the form-feed character.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    hmm, probably does have to do with being a form feed character. You can "cout" those characters though, try it, many fun characters . Though it is probably that which is why C++ is ignoring it when it is being read from file. So, do we know any commands(or methods) that will allow all characters to be read, regardless of position in relation to the number 32? Ther is bound to be one out there, but i don't know it. Perhaps with this knowledge another google search is in order. Will try.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    @Ranorith
    If you've only viewed this character in Notepad how do you know it's ASCII value? You cannot rely on what you see on the screen in Notepad for this, instead, you'd need a proper hex editor.

    Also, checkout IO manipulator noskipws.
    http://www.cplusplus.com/ref/iostrea...e/_skipws.html
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    I know it's ASCII value because i specified it when putting it in Notepad (ie, i created that file using the same program, and specified the ASCII value)
    I have also confirmed that it is the character of the correct value by outputting to the screen (cout) the character of the same ASCII value

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  2. File i/o problem
    By tezcatlipooca in forum C++ Programming
    Replies: 18
    Last Post: 01-01-2007, 09:01 AM
  3. File I/O problem
    By Onions in forum C++ Programming
    Replies: 41
    Last Post: 02-24-2006, 04:32 PM
  4. File I/O problem
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 12
    Last Post: 09-03-2005, 01:14 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM

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