GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Jul-2005, 23:18
vishi vishi is offline
New Member
 
Join Date: Jul 2005
Posts: 2
vishi is on a distinguished road

copying a portion of Binary file into another Binary file


Hi friends,
I have got a binary file with large no of records. I have to copy a portion of that file in another binary file.I am trying to read from the first one and write into the second oneusing for loop for certain no. of records but its not working. Can I do like this?Can anyone help me out?
  #2  
Old 24-Jul-2005, 01:53
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Quote:
Originally Posted by vishi
Can I do like this?Can anyone help me out?

Yes, you can do it that way.

May be you could post the code you have written so far for copying with the problem that you are facing with it.
  #3  
Old 24-Jul-2005, 04:33
kobi_hikri's Avatar
kobi_hikri kobi_hikri is offline
Regular Member
 
Join Date: Apr 2005
Location: Israel
Posts: 431
kobi_hikri has a spectacular aura aboutkobi_hikri has a spectacular aura about

Try to give an example code.


Quote:
Originally Posted by vishi
I am trying to read from the first one and write into the second oneusing for loop for certain no. of records but its not working. Can I do like this?

Just a short example :
CPP / C++ / C Code:
char current_line[256];
//Change this to the number of lines you wish to read.
int   max_lines = 100;
int   current_line = 0;
FILE *ifp;
ifp = fopen("x.y","r");
if (ifp != NULL)
   while (fgets(current_line,256,ifp) && (current_line < max_lines))
      {
         //Do your stuff here.
         current_line++;
      }

This is the basic idea. Post your code and errors for further help.

Best regards,
Kobi.
  #4  
Old 24-Jul-2005, 06:01
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 313
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
I wrote this file copy program a while back using low-level I/O functions creat(), open(), read() and write(). Source file is read in chunks of predefined size and written into the new file. New file, if not available is created by the program.

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
        if (argc!=3)
        {
                printf("\nUSAGE: %s <source file name> <destination file name>\n",argv[0]);
                exit(1);
        }

        int src_handle,dst_handle;
        char buffer[512];
        const int bytes_read=512;
        int rd,wr,bytes_copied;
        struct stat file_attr;
        int file_exist=0;
        int overwrite=0;
        char line[10];
        char choice[5];
        char ch;

        /* Read source file attributes */
        if ((stat(argv[1],&file_attr))<0)
        {
                perror("Stat Error");
        }

        /* Open Source file for reading */
        if ((src_handle=open(argv[1],O_RDONLY))==-1)
        {
                perror("Source File open read");
                exit(1);
        } 

        /* Open/Create destination file for reading */
    if ((dst_handle=open(argv[2],O_WRONLY))!=-1)
    {
                /* File already exists */
                file_exist=1;
    } else {
                        if ((dst_handle=creat(argv[2],00666))==-1)
                        {
                                perror("Destination File create");
                                exit(1);
                        } 
                        if ((dst_handle=open(argv[2],O_WRONLY))==-1)
                        {
                                perror("Destination File open write");
                                exit(1);
                        } 
        }

        //printf("\ncheck1 file_exist: %d Overwrite: %d \n", file_exist,overwrite);
        if (file_exist)
        {
                /* Check if user wants to overwrite */
                printf("Destination file exists.Overwrite?[Y]es/[N]o: ");
                fgets(line,sizeof(line),stdin);
                sscanf(line,"%s",choice);
                //printf("choice %c\n", choice);
                if ((strcasecmp(choice,"No")==0) || (strcasecmp(choice,"n")==0))
                {
                        ch='n';
                } else if ((strcasecmp(choice,"Yes")==0) || (strcasecmp(choice,"y")==0))
                {
                        ch='y';
                } else {
                        ch='n';
                }
                //choice=getchar();
                if (toupper(ch)=='Y')
                {
                        overwrite=1;
                }
        }

        if (overwrite || !(file_exist))
        {

                        memset(buffer,'\0',512);
                        bytes_copied=0;

                        /*Start copying*/
                        while ((rd=read(src_handle,buffer,bytes_read*(sizeof(char))))!=0)
                        {
                                //printf("\nrd:= %d\n",rd);
                                if (rd==-1)
                                {
                                        printf("\nError reading source file\n");
                                        exit(1);
                                }
                                printf("!");
                                bytes_copied+=rd;
                                wr=write(dst_handle,buffer,rd*(sizeof(char)));
                                //printf("\nwr:= %d\n",wr);
                                if (wr==-1)
                                {
                                        printf("\nError writing to destination file\n");
                                        exit(1);
                                }
                        }

                        if ((chmod(argv[2],file_attr.st_mode))<0)
                        {
                                perror("chmod error");
                        }
                        if ((chown(argv[2],file_attr.st_uid,file_attr.st_gid))<0)
                        {
                                perror("chown error");
                        }
                 printf("\n");
                 printf("%d bytes copied.\n",bytes_copied);
        } else {
                printf("File not copied.\n");
        }
 return 0;
}

I have tested this on linux.

Quote:
[root@linux snoopy]# ./fcopy intf1.c intf.c
!!!!!!!
3129 bytes copied.
[root@linux-outside snoopy]# ls -la intf*
-rwxr-xr-x 1 root root 13245 Jul 24 03:08 intf
-rw-r--r-- 1 snoopy snoopy 3129 Jul 24 03:45 intf1.c
-rw-r--r-- 1 snoopy snoopy 3129 Jul 24 03:59 intf.c
[root@linux snoopy]#
  #5  
Old 24-Jul-2005, 23:27
vishi vishi is offline
New Member
 
Join Date: Jul 2005
Posts: 2
vishi is on a distinguished road
I have written this code

CPP / C++ / C Code:
#include<stdio.h>
void main()
{
		struct Data
		{
		char name[4];
		int value[6];
		};
		struct Data person[5]={
			{"Aya",80},
			{"hUA",48},
			{"AdA",70},
			{"sTA",90},
		
		};

	FILE* fptr;
	FILE* sptr;
	fptr=fopen("C:/file","a+b");
	sptr=fopen("C:/gfile","a+b");
	fwrite(&person,sizeof(Data),1,fptr);

	while(feof(fptr))
	{
	fread(&person,sizeof(Data),1,fptr);
	fwrite(&person,sizeof(Data),1,sptr);
	fread(&person,sizeof(Data),1,fptr);
	}
	fclose(fptr);
	fclose(sptr);
}

But this is not working .somethimg else is getting written in the other file
Last edited by LuciWiz : 25-Jul-2005 at 01:29. Reason: Please insert your C code between [c] & [/c] tags
  #6  
Old 25-Jul-2005, 09:05
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
http://www.eskimo.com/~scs/C-faq/q12.30.html

[edit]Why it's bad to use feof() to control a loop -- but you got it backwards anyway.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
Re: Programming Techniques WaltP C Programming Language 0 10-Mar-2004 00:56

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:12.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.