1 Replies - 8627 Views - Last Post: 17 January 2009 - 07:33 AM Rate Topic: -----

#1 Slobodin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-January 09

Concatenate files

Posted 17 January 2009 - 07:00 AM

Hi, I'm trying to write a program to archive, basicaly concatenate some files from a folder. The names of the files are obtained from the file_ls file which contains the result you get when you run ls -la in linux. It's lines of text like this:

drwxr----- 13 slobo slobo 2048 2009-01-16 22:44 ..
-rw-r--r-- 1 slobo slobo 1646 2008-12-15 09:42 usermap.txt


The program should concatenate the files upon inputing the following command:

load file_ls usermap.txt archivename


So it looks into file_ls, finds all the files, then concatenates their content and writes it in the file archivename. The program should also write a header for each of the files, before the content of the file itself, like the tar specifications, that's where I'm going to use usermap.txt. But I'm not worrying about that now, first I'm trying to get it to just concatenate the files ie add their content to the archivename.

My code looks like this :

#include<stdio.h>
#include<string.h>
#define MAX 1000
FILE *s, *d;
void copy( FILE *d, FILE *s)
{
	int c;
	char zone[MAX];
	while((c=fread(zone, 1, MAX, s))>0)
		fwrite(zone, 1, c, d);
	
}
	

void load(char *archivename)
{
	char *x;
	long len;
	FILE* pf = fopen("file_ls", "rb");
	fseek(pf, 0, SEEK_END);
	len=ftell(pf);
	fseek(pf, 0, SEEK_SET);
	x=(char*)malloc(len);
	fread(x, 1, len, pf);
	char *t, *u;
	t=strtok(x, "\n");
	while(t!=NULL){
		if(t[0]=='-')
		{
			u=strchr(t, ':');
			u+=4;
			printf("This is the name of a file from file_ls \n");
			puts(u);
			puts(t);
			s = fopen(u, "r");
			d = fopen(archivename, "a+");
			copy(d,s);
		}
	t=strtok(NULL, "\n");
	}
	
}

int main(){
	char *command, *archivename, *filename, *p;
	int i, j, n, ok=0;
	fgets(command, 100, stdin);
	
	p="load file_ls usermap.txt";
	n=strlen(p);
	if(strncmp(command, p, n )==0)
	ok=1;
	if(ok==1)
	{
	archivename=command+n+1;
	printf("The name of the archive is: \n");
	puts(archivename);
	printf("  Launching load!!!");
	load(archivename);
	}
				fclose(s);
	fclose(d);
	return 0;
}


I need some help with this. It compiles ok, but it doesn't work, doesn't copy anything. I think the copy function is alright, because it worked in other, simpler programs. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Concatenate files

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6106
  • View blog
  • Posts: 23,653
  • Joined: 23-August 08

Re: Concatenate files

Posted 17 January 2009 - 07:33 AM

You're not checking to see if any of your files opened properly, and you're not closing your files when you're done. That's a couple of issues I see right away.

EDIT: Oh wait, you do close them at the very end (globals...yuck!). You should close the reader when you're done with it. If there are a lot of files to read, you may run out of file descriptors, and it's just a good practice.

EDIT 2: This isn't right either:
FILE* pf = fopen("file_ls", "rb");

Why open a text file in binary mode?

This post has been edited by JackOfAllTrades: 17 January 2009 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1