473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Copying a file

I have just started programming in c
i started to make a program to copy contents from one file to another charcter by character. i made the prog but it is not working..
can u plz tell me the error in the code..?
my code is
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main()
  4.  
  5. {
  6.  
  7. char a;
  8.  
  9.  
  10. FILE *fi, *fo;
  11.  
  12.  
  13. fi= fopen ("sohil", "r");
  14.  
  15. fo= fopen ("ab.c", "w");
  16.  
  17. while (1)
  18.  
  19.     {
  20.  
  21.     fscanf (fi, "&c", &a);
  22.  
  23.     if (a==EOF)
  24.  
  25.             {
  26.  
  27.          break;
  28.  
  29.             }
  30.  
  31.     else
  32.  
  33.         {    
  34.  
  35.         fprintf (fo, "%c", a);
  36.  
  37.         }
  38.  
  39.     }
  40.  
  41. fclose (fi);
  42.  
  43. fclose (fo);
  44.  
  45. }
Mar 4 '07 #1
6 2035
willakawill
1,646 1GB
Hi. this is the introductions forum. Welcome to TSDN. You will have a much better chance at getting a reply to your question if you post it in the relevant forum which you will see to the right of this message. Just click on c++/c and copy your message there.
Mar 4 '07 #2
I have just started programming in c
i started to make a program to copy contents from one file to another charcter by character. i made the prog but it is not working..
can u plz tell me the error in the code..?
my code is
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. main()
  4.  
  5. {
  6.  
  7. char a;
  8.  
  9.  
  10. FILE *fi, *fo;
  11.  
  12.  
  13. fi= fopen ("sohil", "r");
  14.  
  15. fo= fopen ("ab.c", "w");
  16.  
  17. while (1)
  18.  
  19.     {
  20.  
  21.     fscanf (fi, "&c", &a);
  22.  
  23.     if (a==EOF)
  24.  
  25.             {
  26.  
  27.          break;
  28.  
  29.             }
  30.  
  31.     else
  32.  
  33.         {    
  34.  
  35.         fprintf (fo, "%c", a);
  36.  
  37.         }
  38.  
  39.     }
  40.  
  41. fclose (fi);
  42.  
  43. fclose (fo);
  44.  
  45. }
I would recommend doing it this way:
1- Open Files. The original and the copy.
2- Declare a character var.
3- Using fgetc() and fwrite() copy content from one to the other
4- close files and you are done.

If this does not get banned, i will post my idea.

#include libraries //don't remember which ones

main(){
char a;
FILE *first,*second;
first=fopen("First.txt","r");
second=fopen("Second.txt","w");
while(!feof(first)){
a=fgetc(first);
fwrite(&a,1,1,second);
}
}
Mar 5 '07 #3
got ur idea dude...thnx...there's still 1 doubt tho...
what do the two 1's in fwrite() imply..??
Mar 5 '07 #4
r035198x
13,262 8TB
Changed title.
Please use a more problem specific title.
Mar 5 '07 #5
got ur idea dude...thnx...there's still 1 doubt tho...
what do the two 1's in fwrite() imply..??
fwrite(&a,1,1,file);
The first parameter is what to be written
The second One is how many bytes are needed for it. Remember a character is a byte, so if you want to write "hello" you have to write 5, otherwise it will split your word. If you write a higher number you will see unexpected characters. The third parameter is how many times you want to write the specified text. The last is the file you want to write in
Mar 5 '07 #6
got ur idea dude...thnx...there's still 1 doubt tho...
what do the two 1's in fwrite() imply..??
Remember to write & before your variable or string.
If you want to write a fixed string like "hello" all the times, you have
to write the word between quotes (").
fwrite(&filecontent,5,1,file);
fwrite("Hello World",11,1,file);

Hope this helps!
Mar 5 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
4
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
10
by: Martin Ho | last post by:
I am running into one really big problem. I wrote a script in vb.net to make a copy of folders and subfolder to another destination: - in 'from.txt' I specify which folders to copy - in...
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
18
by: mike3 | last post by:
Hi. I have an interesting problem. The C program presented below takes around 12 seconds to copy 128 MB of data on my machine. Yet I know the machine can go faster since a copying done at a...
6
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar,...
4
by: Jim Barlow | last post by:
Does anyone know why K&R2 uses the term "File Copying" at this point (1.5.1)? Also, in the K&R2 answers to exercises maintained by Richard Heathfield, for Listing KRX113 Mr Heathfield repeatedly...
13
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to...
2
by: raylopez99 | last post by:
Beware newbies: I spent a day before I figured this out: copying a bitmap (image) file to file is not quite like copying a text file--you have to do some tricks (see below), like using a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.