Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 11th, 2006, 6:21 AM   #1
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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);
	}
}
I think that were I'm going wrong is not reading the file fully but writing to the file while I'm still reading. Is it not possible? Or do I need to do things differently?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Apr 11th, 2006, 8:02 AM   #2
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 321
Rep Power: 4 mackenga is on a distinguished road
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?"
mackenga is offline   Reply With Quote
Old Apr 11th, 2006, 8:22 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 11th, 2006, 6:10 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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;    
}
usage:
./qk < oldfile > newfile

This also works in DOS
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC