reading 1 char at a time

This is a discussion on reading 1 char at a time within the C++ Programming forums, part of the General Programming Boards category; I have a binary file (a rom actualy) and i want to read it 1 char at a time, so ...

  1. #1
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    reading 1 char at a time

    I have a binary file (a rom actualy) and i want to read it 1 char at a time, so that value is converted to a char and saved to another. So if the rom contained
    8A 8B 8C () it would be saved as abc i have the program set up to change and save just not to read 1 char at a time

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    http://www.cppreference.com/cppio_details.html#get

    .... and you may want to open the stream in binary mode.
    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
    Nov 2001
    Posts
    1,348
    One solution is read().

    Code:
    ifstream file;
    file.open("fileName", iso::read | iso::binary);
    file.seekg(0, ios::end);
    int fileSize = file.tellg();
    file.seekg(0, ios::beg);
    char *pChar = NULL;
    
    while (fileSize)
    {
       file.read(pChar, 1);
       ...
       --fileSize;
    }
    Kuphryn

  4. #4
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    Thanks

    thanks i think i no what to do. and i learnt a bunch of stuff

  5. #5
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    Is it me...

    It is me or does this prog instantly crash?
    Code:
    #include <fstream>
    int main()
    {
    int a = 0;
    char *buf;
    ifstream file;
    ofstream file1;
    file1.open("C:/file1.txt", ios::in);
    file.open("C:/file.txt", ios::binary);
    file.seekg(0, ios::end);
    int fileSize = file.tellg();
    while (a<fileSize)
    {
     a=a+1;
     file.read(buf, 1);
     file1<<buf;
    }
    return 0;
    }
    I cant see a thing wrong with the bits i fully understand lol

  6. #6
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    I myself pulled that same mistake not that long ago really.
    The problem is thusly:

    char* buf does not point to anything.

    in order to use it like this:
    Code:
     file.read(buf, 1);
    you have to make it point to a location in memory, where read() can *put* the data.

    umm.. kuphryn.. you didn't initialize your pointer either did you? :P
    unless I missed something..


    Of course since you are only reading one char at a time, would it not be easier to just use
    Code:
     char buf = 'a';
    and
    Code:
     file.get(buf)
    which is better for single-character read anyway...


    one other note, you left file at seekg(0, ios::end) didn't you?
    You might want to set it back to the beggining of the file before you try to read stuff
    note that in kuphryn's post the line that says
    Code:
     file.seekg(0, ios::beg)
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

  7. #7
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    LOL

    so now i have:
    Code:
    #include <fstream>
    int main()
    {
    int a = 0;
    char buf;
    ifstream file;
    ofstream file1;
    file1.open("C:/file1.txt", ios::in);
    file.open("C:/file.txt", ios::binary);
    file.seekg(0, ios::end);
    int fileSize = file.tellg();
    file.seekg(0, ios::beg);
    while (a<fileSize)
    {
     a=a+1;
     file.get(buf);
     file1<<buf;
    }
    return 0;
    }
    doesnt crash out but doesnt work..
    Last edited by krappykoder; 10-22-2002 at 05:55 PM.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I perfer to give a general solution over a code that works. One reason is I want the person to understand the simple concept. Another reason is that it saves me time from looking up references and such.

    krappykoder: I hope you understand the general solution everyone has mentioned. The algorithm is the same.

    Kuphryn
    Last edited by kuphryn; 10-22-2002 at 06:13 PM.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>doesnt crash out but doesnt work..
    Well, what does it do then? Any errors etc, what symptoms?

    >>file1.open("C:/file1.txt", ios::in);
    Always check that the open worked.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    yea

    i have learned quite alot from these posts i had no idea on how to do this b4 now i only have 1 thign wrong with it...it dont work

  11. #11
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    bug

    It does about the same as it did when the cursor was at the end: exits


    i check wether it opend by seeing if the file was made lol

  12. #12
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    odd..

    its doing something, but not writing to the second file

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>file1.open("C:/file1.txt", ios::in);
    >>file1<<buf;
    So, you opened it with ios::in, and then tried to write to it??

    [edit]
    Oh, you did this:
    >>ifstream file;
    how confusing!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    Mmm. Purple.
    Join Date
    May 2002
    Posts
    154

    lol

    it wrote! 00 00 00 00 00 00 00 in fact 275kb worth of 00 00 00

    its starting to work!

    Id have a hard time trying to figure out what the problem is as i have no idea
    Last edited by krappykoder; 10-22-2002 at 06:28 PM.

  15. #15
    Geek. Cobras2's Avatar
    Join Date
    Mar 2002
    Location
    near Westlock, and hour north of Edmonton, Alberta, Canada
    Posts
    113
    first off it's probably a good idea to name your variables something less confusing...

    for instance it would be easier to call the infile "in" and the outfile "out"..

    anyways Hammer is right; you opened your output file *as* an input file by putting in this line:
    Code:
    file1.open("C:\file.txt", ios::in);
    which makes it an in file.. I don't know why it worked though because since it's an ofstream, you shouldn't be able to open it as an infile.. hmm..

    anyway my tips for you are;
    name the variables something easier to remember

    once you do that it should be allot easier to figure out the problem.
    James G. Flewelling
    Rgistered Linux User #327359
    Athabasca University Student (BSc. CIS)

    http://catb.org/~esr/faqs/smart-questions.html
    http://catb.org/jargon/

    http://www.ebb.org/ungeek
    ---GEEK CODE---
    Version: 3.12
    GCS/IT/M d- s+:++ a-->->>+>++>+++>? C++++>$ UL++>++++$ P++>++++ L++>++++$
    E W++ N o? K? w++(--)>--- O? M? V? PS--(---) PE Y+ PGP? t 5? !X R(*)>++
    tv-->! b++(+++)>++++ DI? D+++(---)>++++$ G e*>++$ h++>*$ r!>+++ y?
    ----/GEEK CODE----
    upd: 2005-02-11

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 11:42 AM
  2. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  4. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 12:11 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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