LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-29-2007, 03:59 PM   #1
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
c: go back 1 char (opposite of fgetc())


continued from : http://www.linuxquestions.org/questi...b-file-594787/

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.

i'll appreciate any suggestions.
 
Old 10-29-2007, 05:00 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
You have ungetc, but it's not guaranteed to work with more than one attempt.
 
1 members found this post helpful.
Old 10-29-2007, 05:19 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.)
 
Old 10-30-2007, 08:39 AM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
thanks, what is an NL charecter.

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.
 
Old 10-30-2007, 09:50 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
basically what i am aiming at is moving the file pointer.

thanks,
 
Old 10-30-2007, 12:30 PM   #6
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
NL = New Line = '\n'.

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.
 
1 members found this post helpful.
Old 10-30-2007, 03:15 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Original Poster
Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
thanks, maybe fseek() will give me what i need.

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.

thanks,
 
Old 10-30-2007, 08:05 PM   #8
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
You're very welcome.

Keep us posted on your progress!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
about C++ invalid conversion from 'const char*' to 'char' teoporta Programming 3 07-17-2007 09:24 AM
fgetc - segmentation fault schneidz Programming 9 06-28-2006 02:35 PM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
memory full when reading large files with fgetc() alshishtawy Programming 9 05-30-2004 07:45 AM
fscanf & fgetc help billybob2004 Programming 2 02-04-2004 10:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration