Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined! Vampire5's Avatar
    Join Date
    Jan 2012
    Posts
    22

    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
    Location
    Christchurch, New Zealand
    Posts
    122
    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
    Location
    Arkansas
    Posts
    541
    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.

    Linux User #376741
    Code is Poetry

  4. #4
    Linux Engineer docbop's Avatar
    Join Date
    Nov 2009
    Location
    Woodshed, CA
    Posts
    904
    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
  •