Fputs creating a problem?

Up vote 0 down vote favorite share g+ share fb share tw.

Fseek(ofile, 13, SEEK_SET); fputs("\t", ofile); do { ch = getc(ofile); printf("%c", ch); if(ch == '\n') { fseek(ofile, 13, SEEK_CUR); fputs("\t", ofile); } } while(ch! = EOF); I have written this program which manipulates a file and inserts a \t after a particular position in each line. Whenever I use second fputs it makes file unreadable.

Why is this happening? C file link|improve this question edited Mar 14 '11 at 10:50 asked Mar 14 '11 at 9:48Shweta481112 96% accept rate.

Note that in your if statement, if(ch == '\n') then you are calling getc twice in succession - once in the body of the 'if', and then a second time at the top of the 'do' block. – razlebe Mar 14 '11 at 10:05.

A few things you may want to look at: fputs doesn't insert anything, it overwrites whatever was there. In other words, that TAB character will simply overwrite whatever was originally there. If you want to insert things, you're better off writing a filter-type program which copies characters from one file to another, allowing changes along the way (like inserting if the last newline was 13 characters ago, for example).

Your fseek will change the current position for both writing and subsequent getc operations. That means you need to watch out for lines shorter than you expect. You really should check the return values from fseek and fputs - it is possible that they may fail.

After a getc, the file pointer is at the next character, so make sure it's the one fourteen characters after the newline that you're interested in. Watch out for the final newline in the file. It's unlikely that the seek 13 bytes beyond that will work and you're doing an fputs anyway.

Failing all that, dump out the modified file in hex mode with something like the Linuxy: od -xcb myFileName. Txt and see what the individual bytes are. GEdit is notorious for rejecting files which even have one character out of whack, which is why I use vim for everything :-).

I know what fputs is for – Shweta Mar 14 '11 at 10:14 fseek and fputs both are working. File is manipulated as I want but it doesn't open with text editor. I think its addinng something to the file that is the cause of whole problem – Shweta Mar 14 '11 at 10:16 1 Don't take offence, @Shweta, you came here looking for help.

If you don't want help, I can apply my efforts somewhere else, just let me know. The reason I included that is because you used the word insert. I have no idea what the structure of your data is since you didn't provide that detail.

– paxdiablo Mar 14 '11 at 10:17 I am sorry for the harsh answer but I didn't mean to offend you – Shweta Mar 14 '11 at 10:19 @Shweta: no problems, consider it forgotten. It's unusual for the file to not open in a text editor though. Is it just crashing the editor?

Does the editor give an error of any sort? Which text editor is it? – paxdiablo Mar 14 '11 at 10:20.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions