|
 |
|
 |
 |
09-26-2005, 12:35 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 2
|
file types: reading and writing
dear guru's,
I had a problem in C, and i seems i can't figure out why this piece of codes seems not working properly, kindly help my with my little problem?
/* My problem here is, how can i copy the file into other file name, i just wanted to copy them eliminate the leading spaces leaving only one space each and every after word. Kindly help me figure out what type of condition can i use here?
ex:
from: have' '' '' 'a' '' 'nice' '' '' '' 'day.
to: have a nice day.
*/
================================================== ==
code starts here:
================================================== ==
Code:
/* Copying a file and eliminate white spaces and tab. */
#include <stdio.h>
#include <stdlib.h>
int file_copy( char *oldname, char *newname );
main()
{
char source[80], destination[80];
/* Get the source and destination names. */
printf("\nEnter source file: ");
gets(source);
printf("\nEnter destination file: ");
gets(destination);
if ( file_copy( source, destination ) == 0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
return(0);
}
int file_copy( char *oldname, char *newname )
{
FILE *fold, *fnew;
int c,b,col;
/* Open the source file for reading in binary mode. */
if ( ( fold = fopen( oldname, "r" ) ) == NULL )
return -1;
/* Open the destination file for writing in binary mode. */
if ( ( fnew = fopen( newname, "w" ) ) == NULL )
{
fclose ( fold );
return -1;
}
/* Read one byte at a time from the source; if end of file */
/* has not been reached, write the byte to the */
/* destination. */
while (1)
{
c = fgetc( fold );
/* My problem here is, how can i copy the file into other file name, i just wanted to copy them eliminate the leading spaces leaving only one space each and every after word. Kindly help me figure out what type of condition can i use here?
ex:
from: have' '' '' 'a' '' 'nice' '' '' '' 'day.
to: have a nice day.
*/
if ( !feof( fold ))
fputc(c,fnew );
}
fclose ( fnew );
fclose ( fold );
return 0;
}
================================================== ==
code ends here:
================================================== ==
thank you,
j
__________________
Last edited by redhead : 09-26-2005 at 03:25 AM.
Reason: Cleaning up appearence of code block
|
|
|
09-26-2005, 04:10 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
|
Take a look at this small program
Code:
#include <stdio.h>
int main(int argc, char* argv[])
{
FILE *fin, *fout;
char c;
unsigned short int space = 0;
if(argc != 3)
{
printf("Usage: %s <in_file> <out_file>\n", argv[0]);
return -1;
}
if(!(fin = fopen(argv[1], "r")))
{
printf("Error opening input file %s\n", argv[1]);
return -1;
}
if(!(fout = fopen(argv[2], "w")))
{
printf("Error opening output file %s\n", argv[2]);
return -1;
}
while( (c=fgetc(fin)) && !feof(fin))
{/* read everything in the file char by char */
if(c == ' ' || c == '\t')
{
space = 1;
continue; /* eliminate all whitespaces */
}
/* everything else write it, and add a space infront of a
* word, if we've just come from a line of spaces
*/
if(!fprintf(fout, "%s%c", space?" ":"", c))
{
printf("Error writing to output file %s\n", argv[2]);
fclose(fin);
fclose(fout);
return -1;
}
space = 0;
}
fclose(fin);
fclose(fout);
return 0;
}
At first I wasn't sure you were talking about text files, and was going for a rather advanced solution for any form of binary file..
Anyway feel free to ask, if you encounter more problems, and welcome here at Code Newbie.
|
|
|
09-26-2005, 05:49 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 20
|
Quote:
|
Originally Posted by pre_wreck
/* Get the source and destination names. */
printf("\nEnter source file: ");
gets(source);
printf("\nEnter destination file: ");
gets(destination);
|
Using gets() is a buffer overflow waiting to happen. If you really want to do things this way, at least use fgets(). Also, arbitrary limits on the size of your input/output filenames (or actually buffers / arrays in general) will pretty much always cause you problems later on.
The "proper" way to handle stuff like this for these kind of programs is to either use stdin & stdout to read and write, use command line arguments, or do both. ie, my_prog < infile > outfile, or my_prog infile -o outfile, etc.
Quote:
|
Originally Posted by pre_wreck
if ( file_copy( source, destination ) == 0 )
puts("Copy operation successful");
else
fprintf(stderr, "Error during copy operation");
|
Any particular reason you're using puts() when successful and fprintf to stderr on failure? In general if an operation succeeds you give no output, if it fails you output an error. If you do output status info, send it to stderr. Also, you probably want to add a newline at the end of your output  .
redhead wrote a possible solution already, but since I was bored  :
Code:
#include <stdio.h>
#include <stdlib.h>
#define BUFF_SIZE 4096
int file_copy(FILE *fin, FILE *fout)
{
enum STATE {
STATE_SPACE,
STATE_NEWLINE,
STATE_DEFAULT
} state = STATE_NEWLINE;
size_t num_read = 0, num_write = 0;
char *buff = 0, *ptr = 0;
size_t i = 0;
buff = malloc(BUFF_SIZE);
if (!buff) return -1;
while (!feof(fin))
{
num_read = fread(buff, sizeof(char), BUFF_SIZE, fin);
if (!num_read)
{
fprintf(stderr, "Error in read\n");
free(buff);
return -1;
}
for (i = 0, ptr = buff; i < num_read; i++)
{
switch(buff[i])
{
//case '\t': //Uncomment to strip tabs as well
case ' ':
if (state == STATE_NEWLINE) break;
state = STATE_SPACE;
break;
case '\n':
*ptr++ = buff[i];
state = STATE_NEWLINE;
break;
default:
if (state == STATE_SPACE) *ptr++ = ' ';
*ptr++ = buff[i];
state = STATE_DEFAULT;
break;
}
}
if (!(num_write = ptr - buff)) continue; //Nothing to write
if (!fwrite(buff, sizeof(char), num_write, fout))
{
fprintf(stderr, "Error in write\n");
free(buff);
return -1;
}
}
free(buff);
return 0;
}
int main()
{
if (file_copy( stdin, stdout) != 0)
{
fprintf(stderr, "Error during copy operation\n");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
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
|
|
|
All times are GMT -8. The time now is 02:19 AM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|