![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Read and write to one file
I would like to read and write to one file, that can be done by setting the mode to rb+ in fopen.
What I want to achieve is putting a space before every line. I thought it would work like: #include <stdio.h>
main(int argc, char *argv[])
{
FILE *fp;
void insertspaces(FILE *, FILE *);
argc = 2;
argv[1] = "C:\\main.cpp";
if (argc == 1)
{
printf("insertspaces: no file(s) specified\n", *argv);
return 1;
}
else
while(--argc > 0)
if ((fp = fopen(*++argv, "rb+")) == NULL)
{
printf("insertspaces: can't open %s\n", *argv);
return 1;
}
else
{
insertspaces(fp, fp);
fclose(fp);
}
return 0;
}
/* insertspaces: insert spaces in file ifp to file ofp */
void insertspaces(FILE *ifp, FILE *ofp)
{
int c;
putc(' ', ofp);
while ((c = getc(ifp)) != EOF)
{
putc(c, ofp);
if(c == '\n')
putc(' ', ofp);
}
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 321
Rep Power: 4
![]() |
I find this slightly bizarre, but I've never actually tried to do this before. I believe the problem is that the characters you're writing to the file are overwriting the first couple of bytes of the next line rather than being inserted before them. Like I say, I've never really tried to insert data into a file in this way; I'd generally write this sort of program as a filter and have it read standard input and write the data with space inserted (or whatever processing done) to standard output.
I suspect to insert the data in place you'd have to do a more torturous sort of job, moving the data forward before writing spaces where it had been. This sounds like agony to me, which is probably why I prefer the filter approach. Hope this helps (or that someone else has something more helpful to volunteer!)
__________________
"I'm not a genius. Why do I have to suffer?" |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I didn't look at your code, Ruben, just some comments. Since you aren't dealing with the medium at a low level, when you use the C/C++ libraries, you should view your file as if it were mag tape: one long string of bytes laid end to end. If you wish to make something longer than it currently is, as an in-place modification, you have to read ahead to get data out of the way so you can add bytes where it was. We discussed this half to death on That Other Forum. It can be done, but there's no point unless you're pushed for storage resources. Read, modify, write to temp file. When done rename temp file as original file. If you want to do it purely as a learning exercise, think fifo and bookeeping. You'll need seek and tell. That means you'll need to operate in binary mode if you're on Windows.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
This does not write to a single file but uses files already open by C.
/* unix quick version qk.c*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char tmp[4096]={0x0};
while( fgets(tmp, sizeof(tmp), stdin)!=NULL)
fprintf(stdout, " %s", tmp);
return 0;
}./qk < oldfile > newfile This also works in DOS |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|