This question already has answers here:
Reading a text file backwards in C
(5 answers)
Closed 7 years ago.
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.
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.
Related
This question already has an answer here:
Python-like array filling - C equivalent
(1 answer)
Closed 2 years ago.
I'm looking for an "append" (Python) or "push_back" (C++) equivalent in C to fill an array of strings (char). Does it exists?
The question doesn't make sense because you cannot add elements to an array in C. In C, you set the size of the array when you create it, and the size cannot change.
If you want to "append", you have to create a big array (e.g. 1000 items), and use a separate variable to remember how many items you're actually using (the rest are spare).
This question already has answers here:
Can someone explain hex offsets to me?
(6 answers)
Closed 8 years ago.
I'm trying to understand what an offset in a hex dump is. In particular, what purpose does an offset serve? I have googled many times but not found anything.
The offset describes where something is in the file. You can obtain and jump to offsets in code using lseek(2) or fseek(3), depending on which I/O system you're using.
This question already has answers here:
C dynamically growing array
(10 answers)
Closed 9 years ago.
How do I read in a c-string in to a character array without knowing the size of the string that the user will enter ?
Without any code or further description of your issue it's hard to know what you're trying to achieve, but, one of the following might be appropriate for your needs:
Use a preallocated array of some maximum size you know is greater than the number of characters that will be entered.
Create an empty std::string, and then use the string "+=" operator for each character entered. Then you can convert back to an array using the c_str() method.
This question already has answers here:
How can I read and parse CSV files in C++?
(39 answers)
Closed 9 years ago.
I have CSV files that contain 2 columns of x and y coordinates, that are the positions of particles.
I want to read these files and be able to use the data inside to calculate a function (I mean I want use the coordinates to calculate the pair distribution function g(r)).
I have the algorithm to calculate this function but I need help with how to access the CSV files, and be able to read and use the data inside. I'm new to programing in C, can anyone help?
You use a library, such as libcsv.
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
How to convert struct to char array in C
Portable way of writing a C struct to a file (Serialisation for C)
I am looking for converting a structure to byte array in C and i was confused in doing that.Please show me a right way in achieving that. Thanks in advance.
A structure is a byte array. it starts at &mystruct and has the length of sizeof(mystruct_type) bytes.
If the binaries are to long or do contain gaps, check the #pragma pack settings.
hth
Maro