Results 1 to 10 of 10

Thread: [RESOLVED] Problems reading integers and strings from txt file ...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Resolved [RESOLVED] Problems reading integers and strings from txt file ...

    sorry for being thick ... but im trying to save data to a text file and need to store my integers as char values how do i do this

    regards
    john
    Last edited by Hack; Apr 13th, 2006 at 12:23 PM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: char[] to int ? c++ and int to char[]

    i dont understand ... this is the problem im having ....

    im storing a text string that says 'Duolc'

    and when i read it its being returned as 'Doud' ??

    even when i debug and step through my code the value says its 'Duolc' but when its printed to the screen it outputs 'Doud' ???

    any ideas ?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: char[] to int ? c++ and int to char[]

    and my first post refers to problems im having reading integers from and the second text strings ....

    CNAME
    Duolc
    CATTACK
    100
    CDEFENCE
    0
    CPSTRENGTH
    0
    CMSTRENGTH
    0
    CHP
    100
    CMP
    100
    ^^ there's the file structure

    all i want todo is read cname into a char variable and the rest of the vars to integers

    ^^ maybe that'll be of further use

  4. #4
    Frenzied Member aewarnick's Avatar
    Join Date
    Dec 2002
    Posts
    1,037

    Re: Problems reading integers and strings from txt file ...

    When you write and read your file, use b. My wife if kicking me off off, gotta go.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Problems reading integers and strings from txt file ...

    .... ???

    please get back to me mate im still really stuck

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Problems reading integers and strings from txt file ...

    ahhh do you mean read by byte ?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Problems reading integers and strings from txt file ...

    ... nope still cant figure it out ??

  8. #8
    Lively Member
    Join Date
    Nov 2005
    Posts
    68

    Re: Problems reading integers and strings from txt file ...

    Please post your code. Just supposing doesnt help here.
    "bla, bla,... exists number M so for each n > M bla, bla..." Exists? Where is it? (Kronecker said...)

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Problems reading integers and strings from txt file ...

    VB Code:
    1. int OpenCharacter (char *FileName) {
    2.  
    3.     //ResetCharacterVars();
    4.  
    5.     char myline[]=""; //
    6.     char fname[256]="";
    7.     int iReturn=0;
    8.  
    9.     //strcat(fname, szDirectory);
    10.     strcat(fname, "Characters/");
    11.     strcat(fname, FileName);
    12.     strcat(fname, ".chr");
    13.     ifstream myfile (fname);
    14.     if (myfile.is_open())
    15.     {
    16.         bool FinishedReading=FALSE;
    17.         while (!myfile.eof() )
    18.         {
    19.         char myLine[100];
    20.         int intVal=0;
    21.             //string line; //[256]="";
    22.             if (!FinishedReading){
    23.            
    24.             myfile >> myLine; // skip CNAME identifier
    25.             myfile >> myLine; // read real cname
    26.             strcpy(CSNAME, myLine); // copy to string
    27.            
    28.             myfile >> myLine; // Attack points marker
    29.             myfile >> CSATTACK; // read real value
    30.            
    31.             myfile >> myLine; // defence points marker
    32.             myfile >> CSDEFENCE; // read real value
    33.  
    34.             myfile >> myLine; // Strength points marker
    35.             myfile >> CSPSTRENGTH; // read real value
    36.  
    37.             myfile >> myLine; // Magic points marker
    38.             myfile >> CSMSTRENGTH; // read real value
    39.  
    40.             myfile >> myLine; // init hp points marker
    41.             myfile >> intVal; // read real value
    42.  
    43.             myfile >> myLine; // init hp points marker
    44.             myfile >> intVal; // read real value
    45.  
    46.             FinishedReading=TRUE;
    47.             }
    48.             myfile >> myLine;
    49.         }
    50.         myfile.close();
    51.  
    52.  
    53.         iReturn=1;
    54.     }
    55.  
    56.     else {
    57.         //cout << "Unable to open character file";
    58.         //getch();
    59.         iReturn=0;
    60.     }
    61.     return iReturn;
    62. }

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Location
    UK
    Posts
    127

    Re: Problems reading integers and strings from txt file ...

    its cool i got it working .... lol ...

    defined a 'character' structure in another file and it now seems to be reading writing to the structure and to the screen fine

    thanks for your time any way

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width