0

I am reading a one line string from a file and I need to read it into an array in reverse order. How can I accomplish this?(I am using C).

So, my file looks like:

ACGTGCGATCGATCGATCGATATCGATCGTCTGCTTAAGCTC

And I want my array of chars that I read into to look like:

CTCGAA...

Thanks in advance.

  • Not a portable solution, but you may want to try `fseek(..., SEEK_END)` to position your position indicator to the end of the file and can read backwards. Check this for details http://www.gnu.org/software/libc/manual/html_node/File-Positioning.html – fnisi Feb 18 '16 at 00:34

1 Answers1

1

Read the file front-to-back into the array, then reverse the contents of the array in place. That's the best way to do that as reading a file backwards is a very slow operation.

fuz
  • 88,405
  • 25
  • 200
  • 352
  • Thanks for answering OPs question. I don't understand the jokes, down votes and close votes. The guy has a simple need and asks a simple question. Either answer, or move on. – Traveling Tech Guy Feb 15 '16 at 15:57
  • 1
    @TravelingTechGuy The question is downvoted because it can be answered by typing a single sentence into a search engine of choice. It's a problem you can easily solve yourself with five minutes of thinking. – fuz Feb 15 '16 at 16:01
  • I get that, but sometimes people don't really know what to search for, or they think there's a built-in way to do what they need (i.e. read a file backwards) that they are missing. Not all of us get here with a programming background (this guy for example looks like he's trying to read genetic data). Even a one-line answer like yours is enough to set him on the right track. And at any rate, this question is not "too broad". – Traveling Tech Guy Feb 15 '16 at 16:09
  • @TravelingTechGuy See the duplicate question. If OP had just looked at the suggested similar questions when asking his question, hew would found the duplicate linked to by user694733. – fuz Feb 15 '16 at 16:16
  • @TravelingTechGuy googling the exact title of the question is a start. No need to be a programmer for that. – Jabberwocky Feb 15 '16 at 16:18