ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi, i would like to go back a few chars after comparing a string.
the end result i am looking for is to put a new line before the existance of certain 3 charecter flags.
i am dealing with a file with ascii-chars mixed in with binary. i already have a program to substitute spaces for non-ascii bytes for padding but now i would like to columnate it so that it is more readable. each line is anywhere from 2000 to 6000 bytes wide.
From your description of your problem it sounds like you're trying to modify the physical contents of a file without rewriting the whole file. This seems logically impossible for a sequential sequence of characters.
On the other hand, if all you want to do is display the file contents (or write to a new file), adding the NL character as needed, consider buffering you input and only writing from the buffer when you've determined that you should. (With proper pointer arithmetic the buffer could be a circular buffer, and might not need to be too large.)
i had something that ideally worked for a while. it would go thru and substitute a space for anything not between ascii-32 and ascii-126 (otherwise printf(c).
also it would loop thru fgetc 2456 times if the line began with 'aaa', 3325 times if it began with 'abc', and 5980 times if it began with 'bbb'. so it is not impossible.
today i got a 'bbb' line with 5978 charecters on the 208th line so the rest of the output was out of sync. i figured instead of hardcoding the line widths i could circumvent that.
i'm trying to see if aix has ungetc; still looking for ideas.
I'm not sure how you're doing this, but why not buffer the input?
If you know the length of each read-in line, just throw each character into a buffer array. Also, there's nothing saying you can't create a buffer array of a generic length (let's say 10000 for argument's sake). You just stop adding characters to that array upon hitting a newline character - '\n'.
By this point, you need not worry with fgetc() and ungetc(), it just becomes a matter of changing your array index.
Also, if you don't want to buffer line-by-line, you could just buffer the entire file (but this could get problematic REALLY quickly depending on the size of the file, in which case you could just use a smaller buffer, ie. 512 or 1024), and the array index of the buffer will correspond to the current byte location in the file. If you're using this method, don't forget to create a integral variable for the byte offset:
Code:
#define BUFFERSIZE 512
...
int offset = 1;
char buffer[BUFFERSIZE];
...
...
/* filling buffer */
...
...
/* flushing buffer */
for (int x = 1; x <= offset; x++)
for (int y = 0; y < BUFFERSIZE; y++)
putchar(buffer[y]);
...
EDIT:
This may be completely un-related, but I know Python has a seek() function for open files, that allows you to change the byte location you are at in the file.
Seeing as Python is written in C, you will more than likely find something there. But, seeing as you probably don't have time to crunch through the countless lines of Python library code, rifle off some Google searches looking for a similar seek() function in C.
Last edited by indienick; 10-30-2007 at 03:08 PM.
Reason: Brainfart.
something i should add is since there is binary charecters in my input there are some ascii-10's (NL's) sparsed throughout each line. my experience is that fgets()/ fscanf() read up to a new line.;im transposing ascii-10 to ascii-32.
seems like i have to read up on my c.
ill play around with all your suggestions; this is really helpful.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.