Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: z80man on February 03, 2011, 01:49:51 am

Title: File output
Post by: z80man on February 03, 2011, 01:49:51 am
So I've been working on an app signer program for the Prizm when I came along a problem. What I'm trying to do is copy one file and output to another. I've already tried using the << operator, the put function, and the write function, but my compiler (Visual C++ 2010) does not support these.  :P  None of the shown examples work. Also VC++ 2010 does not support the seekp method. Any ideas.  ???
Code: [Select]
ifstream app(filename, ios::binary);
char Convbuf;                                        // buffer to hold one byte at a time from the Conversion app
for( int counter = 0  ; counter < 0x7000; counter++)
{
Conv.read(&Convbuf,1);
app << Convbuf;
}
Code: [Select]
ifstream app(filename, ios::binary);
char Convbuf;
Conv.seekg(0, ios::beg);
app.seekp(0, ios::beg);
Conv.read(&Convbuf,1);
for( int counter = 1  ; counter < 0x7000; counter++)
{
app.write(&Convbuf, 1);
Conv.seekg(counter);
app.seekp(counter);
}
Title: Re: File output
Post by: TC01 on February 03, 2011, 08:03:08 am
I would use functions like "fwrite", "fread", "fopen", "fclose", etc: if VC++ 2010 doesn't support them, MSDN is a liar. :P

There's an example on the MSDN page for fread (http://msdn.microsoft.com/en-us/library/kt0etdcs.aspx).
Title: Re: File output
Post by: Binder News on February 03, 2011, 03:55:04 pm
are you sure you are including the files them correctly?
Also, did you put: "using namespace std;" in the file, right after the includes?
Title: Re: File output
Post by: z80man on February 03, 2011, 09:10:20 pm
I would use functions like "fwrite", "fread", "fopen", "fclose", etc: if VC++ 2010 doesn't support them, MSDN is a liar. :P

There's an example on the MSDN page for fread (http://msdn.microsoft.com/en-us/library/kt0etdcs.aspx).
It kinda bugs me that I have to use part of the C library in a C++ program, but oh well. I did try the fwrite, fopen, fread and the program did compile, but the program crashed after running it.
Code: [Select]
ifstream app(filename, ios::binary);
char Convbuf[0x7000];
char Convbuft;
FILE * appstream;
fopen_s(&appstream, filename, "w+t");
Conv.seekg(0, ios::beg);

for( int counter = 0  ; counter < 0x7001; counter++)
{
Conv.read(&Convbuft,1);
Convbuf[counter] = Convbuft;
}
fwrite(Convbuf, 0x7000, 0x7000, appstream);
cin.get();
return 0;
are you sure you are including the files them correctly?
Also, did you put: "using namespace std;" in the file, right after the includes?

Yes the fstream library is included along using namespace std. The problem is that fstream in VS will support some methods such as open, close, and read, but write is not included. This could be because I'm using the express version of the software and I don't want to pay $750 for the professional edition.
Title: Re: File output
Post by: calc84maniac on February 03, 2011, 09:19:51 pm
Actually, I think it's because you defined the file object as ifstream, which only supports input operations. Try using ofstream for outputting.
Title: Re: File output
Post by: z80man on February 04, 2011, 12:07:37 am
Actually, I think it's because you defined the file object as ifstream, which only supports input operations. Try using ofstream for outputting.
I've now tried both ofstream and fstream and neither support the write method. I've decided to post my full code and have commented the part that generated an error. It is near the end and is the only error in the code. The rest I have tested and works fine.
Code: [Select]
// app_signer.cpp : Defines the entry point for the console application.


#include "stdafx.h"
#include <fstream>
#include <iostream>
using namespace std;

int main(void)
{
ifstream codename("progname.txt", ios::in|ios::binary);
if(!codename.is_open())
{
cout << "cannot find progname.txt";
cin.get();
exit(1);
}
char hexfile[50];
while(!codename.eof())
{
codename.getline(hexfile, 49);
}
codename.close();
ifstream code(hexfile, ios::in|ios::binary);
if(!code.is_open())
{
cout << "cannot find " << hexfile;
cin.get();
exit(1);
}
int startcode;
int stopcode;
int sizecode;
startcode = code.tellg();
code.seekg (0, ios::end);
stopcode = code.tellg();
sizecode = stopcode - startcode;
code.seekg (0, ios::beg);
ifstream Conv("Conv.g3a", ios::in|ios::binary);
if(!Conv.is_open())
{
cout << "cannot find Conv.g3a";
cin.get();
exit(1);
}
char filename[50];
for(char counter = 0; counter < 50; counter++)
{
if(hexfile[counter] == '.')
{
for(char counter2 = 0; counter2 < counter; counter2++)
{
filename[counter2] = hexfile[counter2];
if(counter2 + 1 == counter)
{
filename[counter2 +1] = '.';
filename[counter2 +2] = 'g';
filename[counter2 +3] = '3';
filename[counter2 +4] = 'a';
for(char counter3 = counter2 +5; counter3 < counter; counter3++)
{
filename[counter3] = ' ';
}
break;
}

}
break;
}
}
ifstream app(filename, ios::binary);
char Convbuf;
ofstream app(filename, ios::out|ios::binary);
app.seekg(0, ios::beg);
Conv.seekg(0, ios::beg);
Conv.read(&Convbuf, 1);
for( int counter = 1  ; counter < 0x7001; counter++)
{
app.write(&Convbuf, 1);                          //ERROR: write method not recognized
Conv.seekg(counter);
app.seekg(counter);

}
cin.get();
return 0;
}
Edit: Okay I replaced the libraries with that of the Borland compiler which fixed the write issue, but now a new error pops up that says ifstream, fstream, ofstream is ambigious. ???
Title: Re: File output
Post by: Binder News on February 06, 2011, 08:52:09 am
Here is where you should go http://www.cplusplus.com/reference/iostream/ifstream/