Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Standard C, C++
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 09-26-2005, 12:35 AM   #1 (permalink)
pre_wreck
Registered User
 
Join Date: Sep 2005
Posts: 2
pre_wreck is on a distinguished road
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
pre_wreck is offline   Reply With Quote
Old 09-26-2005, 04:10 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,680
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 09-26-2005, 05:49 AM   #3 (permalink)
Locutus
Registered User
 
Join Date: Aug 2005
Posts: 20
Locutus is on a distinguished road
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; }
__________________
Locutus is offline   Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading chunks of data in a servlet??? j.gohel Java 2 09-10-2005 01:40 PM
text file recursive string reading C toblerone Standard C, C++ 4 08-04-2004 10:05 PM


All times are GMT -8. The time now is 02:19 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle