The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
file merger
Discuss file merger in the C Programming forum on Dev Shed. file merger C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 15th, 2002, 04:19 AM
|
|
Junior Member
|
|
Join Date: Jun 2002
Location: Australia
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
file merger
Im writing a file merger mainly for concating phrack issues =P - but it compiles and doesnt wanna open files and keeps crashing =\ quite odd - if anyone can help would be appriecated - here is the code :
PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void readstdin(char *array, char *strbuff);
int main()
{
int i, c2;
char c = 'y';
char *base;
char *filem;
char *strbuff = ( char* ) calloc( 1024, sizeof(char) );
FILE *fp1;
FILE *fp2;
printf("Choose base file ?\n>>");
readstdin(base, strbuff);
free( strbuff );
fp1 = fopen(base, "a+");
while( !fp1 ) {
printf("- failed opening file -\n");
printf("Choose base file ?\n>>");
readstdin(base, strbuff);
free( strbuff );
fp1 = fopen(base, "a");
}
while( c != 'n' ) {
printf("Choose file to merge ?\n>>");
readstdin(filem, strbuff);
free( strbuff );
fp2 = fopen(filem, "rb");
while( !fp2 ) {
printf("- failed opening file -\n");
printf("Choose file to merge ?\n>>");
readstdin(filem, strbuff);
free( strbuff );
fp2 = fopen(filem, "rb");
}
while( (c2 = fgetc(fp2)) != EOF ) {
fputc(c2, fp1);
}
fclose(fp2);
printf("\nWould you like to merge a file ?");
scanf("%c", c );
}
fclose(fp1);
system("pause");
return 0;
}
void readstdin(char *array, char *strbuff)
{
scanf( "%s", strbuff );
array = ( char* ) calloc( strlen(strbuff), sizeof(char) );
strcpy( array, strbuff );
}
NB: used PHP tags but its C
|

November 15th, 2002, 04:47 AM
|
|
Junior Member
|
|
Join Date: Nov 2002
Location: australia
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
HI ALEN
Well i would help u if u werent such a newb
how found da best book on python, it called "core python programming"
heard of it??
|

November 15th, 2002, 12:17 PM
|
|
Contributing User
|
|
Join Date: Oct 2000
Location: Back in the real world.
|
|
one problem i can see at the first glance:
PHP Code:
void readstdin(char *array, char *strbuff)
...
array = ( char* ) calloc( strlen(strbuff), sizeof(char) );
you assign to the char*, but this makes "array" a local copy ( !) in the scope of "readstdin()".
you need to pass a pointer to the pointer or a reference to the pointer.
eg.
PHP Code:
void readstdin(char **array, char *strbuff)
{
scanf( "%s", strbuff );
*array = ( char* ) calloc( strlen(strbuff), sizeof(char) );
strcpy( array, strbuff );
}
or you can allocate the memory in the main function instead before calling readstdin()
|

November 15th, 2002, 07:41 PM
|
|
Junior Member
|
|
Join Date: Jun 2002
Location: Australia
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
hmm - I tried that but same thing: it gets the base file now and crashes when opening the file to be appended.
Plus one minute Dev-C++4 was throwing an error -
d:\my documents\projects\untitled1.c: In function `main':
d:\my documents\projects\untitled1.c:18: warning: passing arg 1 of `readstdin' from incompatible pointer type
d:\my documents\projects\untitled1.c:25: warning: passing arg 1 of `readstdin' from incompatible pointer type
d:\my documents\projects\untitled1.c:32: warning: passing arg 1 of `readstdin' from incompatible pointer type
d:\my documents\projects\untitled1.c:39: warning: passing arg 1 of `readstdin' from incompatible pointer type
d:\my documents\projects\untitled1.c: In function `readstdin':
d:\my documents\projects\untitled1.c:62: warning: passing arg 1 of `strcpy' from incompatible pointer type
- then iI compile again and it does, very odd.
Also wondering if that is a problem why doesnt Dev-C++ catch it ?
Last edited by empty : November 15th, 2002 at 07:46 PM.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|