Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question C++ read individual characters/lines in a file

    I am creating a program that needs to read each single character of a text file (ascii) and convert it to a number. Is there a way I can do this with a for loop or something? I also said lines because I will need to convert those characters to a 3-digit number depending on which character it is.

    If I am not being very clear, please ask, I really want to make this program. Thanks!

  2. #2
    Linux Newbie reginaldperrin's Avatar
    Join Date
    Oct 2010
    Christchurch, New Zealand
    Undoubtedly, yes, a loop of some sort will be needed somewhere.
    I understand that homework-type questions are not answered on here very often, though, for the very reason that they are meant to be your work, not someone elses. Also, one of the best ways to learn is to nut it out for yourself.
    Not to say of course that no-one will help. I think most people would like to see you give it a good shot first, maybe try quite a few methods, and only come back here if you are truly stuck, but not at the beginning of the task.

  3. #3
    Linux Enthusiast gruven's Avatar
    Join Date
    Dec 2004
    Is it just one long string of a text file? Or is each character on a different line?

    Either way is pretty simple, but can take different approaches.

    If it is just one long string of characters, read them into a character array and work with it there (hint: a string can be used as a char array). If it is one on each line, then setup a loop and read each line separately with getline, do the comparison and move on.

    And of course, there are several other ways to do it, those two were on the top of my head.

    Code is Poetry

  4. $spacer_open
  5. #4
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Woodshed, CA
    Also look at an ASCII chart you'll see part of your solution done for you.

Posting Permissions

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