top_left top_right
bottom_left
Next Event: Unknown | Forum Rules | QGL Website | Event Registration
openFolder AusForums.com
iconwatfolderLineopenFolder LANs
iconwatfolderLineopenFolder QGL
iconwatfolderLineopenFolder QGL Forum
Author
Topic: basic C++ help
thre3dee
Posts: 996
Location: Brisbane, Queensland

i've decided to learn a bit of C++ while im doin uni and i'm trying to copy an existing file.

int main()
{
    FILE *oldFile;
    FILE *newFile;
    char fileOp;
    oldFile = fopen ("test.bmp","rb");
    newFile = fopen ("testcopy.bmp", "wb");
    if (oldFile==NULL) {
        printf("Error opening file");
    }
    else{
        (this is where i'll put the copying code)
    }
    system("pause");
    return 0;
}

anyone know how to use fputc and fgetc to read each byte of 'oldFile' into the variable 'fileOp' and write them out to the file 'newFile'.

my friend showed me how to do it but the .cpp file's on some other comp at uni and he's not available atm.
system
--
Opec
Posts: 2814
Location: Brisbane, Queensland
Is this for Windows C++ or Unix C++?
thre3dee
Posts: 997
Location: Brisbane, Queensland
windows

i'm using visual c++ .NET if thats any help

last edited by thre3dee at 15:02:40 22/Feb/05
Opec
Posts: 2815
Location: Brisbane, Queensland
Try this:


// reading binary file
#include <iostream.h>
#include <fstream.h>

const char * filename = "test.bmp";
const char * outfile = "testcopy.bmp";

int main () {
char * buffer;
long size;
ifstream file (filename, ios::in|ios::binary|ios::ate);
fstream outfile (outfile, ios::out|ios::binary);
size = file.tellg();
file.seekg (0, ios::beg);
buffer = new char [size];
file.read (buffer, size);
file.close();

outfile.seekg(0, ios::beg);
outfile.write(buffer, size);
outfile.close();

delete[] buffer;
return 0;
}




last edited by Opec at 15:26:51 22/Feb/05
thre3dee
Posts: 998
Location: Brisbane, Queensland
didnt work

the file is created but the data isnt copied (just a new bitmap with some black lines in it)

Opec
Posts: 2816
Location: Brisbane, Queensland
heh it worked on my Unix box... of course it wouldn't work in Windows.
thre3dee
Posts: 999
Location: Brisbane, Queensland
do u know the windows code then?
excuse the newbieness to c++
rodolphe
Posts: 99
Location: Brisbane, Queensland
I was going through some old texts today and am selling them all... I have 4 programming texts, very basic ones. If Opec dun help you, you can buy the books from me :D
Strik3r
Posts: 1088
Location: Gold Coast, Queensland

Avoid using the FILE structure.. if your at uni you will be taught fstream's

Using as much of your code as possible, heres something that should work.

#include // fstream
#include // iostream

int main()
{
ifstream oldFile;
ofstream newFile;

char LineBuff[256];

oldFile.open(oldfilename.bmp);
if (oldFile==NULL)
{
printf("Error opening file");
}
else
{
newFile.open(newfilename.bmp);
if (newFile==NULL)
{
printf("Error outputting file");
break;
}
while(!oldFile.eof())
{
oldFile.getline(LineBuff, sizeof(LineBuff));
newFile.write(LineBuff, sizeof(LineBuff));
}
}
system("pause");
oldFile.close(); // always close files
newFile.close(); // always close files
return 0;
}


I havent tested, but i think it should work.

also, learn to lineup your braces properly, or people will think your a java noob :D

excuse the lack of tabs i dont know how to make them work in forums.. if u cant read it very good, ill put it on rafb.net

last edited by Strik3r at 15:46:14 22/Feb/05
Raven
Posts: 817
Location: Melbourne, Victoria
It might be worth pointing out that everything you've put in the initial post is straight C, not C++.
thre3dee
Posts: 1000
Location: Brisbane, Queensland
thx if it doesnt work then i'll go through the errors and try and fix em.

i'm used to higher level stuff like actionscript and stuff so yeah
stinky
Posts: 492
Location: Brisbane, Queensland
WTF striker? that's not lining up your braces properly, indent them for nested braces. damn Java n00b!
habib
Posts: 336
Location: Brisbane, Queensland
erm....

how about

#include <windows.h>

...

CopyFile("test.bmp","testcopy.bmp");

...


or, if you insist on doing it the way you described:



...
else
{
while (!feof(oldFile))
{
char c = fgetc(oldFile);
fputc(c,newFile);
}
fclose(oldFile);
fclose(newFile);
}
...


pretty sure that will work.
Strik3r
Posts: 1089
Location: Gold Coast, Queensland
WTF striker? that's not lining up your braces properly, indent them for nested braces. damn Java n00b!


......

it wouldnt be because the forum didnt like the tab charecters i copied straight from my compiler would it.....

nitwit

last edited by Strik3r at 23:33:13 22/Feb/05
system
--
Not a new post since your last visit.
New Post Since your last visit
Back To Forum
Advertise with Us | Privacy Policy | Contact Us
© Copyright 2001-2026 AusGamers Pty Ltd. ACN 093 772 242.
Hosted by Mammoth Networks - Australian VPS Hosting
Web development by Mammoth Media.