[QUOTE=Chad Mobile;21083100]See, I know [B]how[/B] to help someone.[/QUOTE]
you just don't do a good job of it
You didn't see my post did you
In my flash 8 platformer- When Object 'player' falls into box i want it to play a noise.
[code]
onClipEvent (enterFrame) {
if (_root.player.hitTest(this)) {
_root.player._x = -504.0;
_root.player._y = -300.0;
_root.score -= 5;
}
}
[/code]
Now, the code above is what i already have on the box. how can i make it so once the object 'play' hits it, then sound (example) awesome.wav plays from library (p.s the current codes i have on it work fine)
thanks :D
(real quick- just in case someone is wondering, its as2)
So nobody has ANY idea what's wrong with my code either?
[editline]11:44PM[/editline]
Well, I did some cleaning and stuff on the code anyways while waiting for someone to come up with why it doesn't work.
[cpp]using System;
using System.IO;
using System.Runtime.Serialization;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace Shamir
{
/// <summary>
/// This is a share to be used with a ShamirEngine object.
/// </summary>
public class Share
{
/// <summary>
/// This creates a Share from a point made up of a long and a BigInteger.
/// </summary>
/// <param name = "x">The X component.</param>
/// <param name = "y">The Y component.</param>
public Share(long x, BigInteger y)
{
X = x;
Y = y;
}
/// <summary>
/// This creates a share from serialized data.
/// </summary>
/// <param name = "serialized">The serialized data of a share.</param>
public Share(byte[] serialized)
{
try
{
MemoryStream memoryStream = new MemoryStream(serialized);
BinaryReader binaryReader = new BinaryReader(memoryStream);
X = binaryReader.ReadInt64();
byte[] temp = new byte[binaryReader.ReadInt32()];
binaryReader.Read(temp, 0, temp.Length);
Y = new BigInteger(temp);
}
catch (Exception)
{
throw new SerializationException("Deserialization failed.");
}
}
/// <summary>
/// This is the X component of this share.
/// </summary>
public long X { get; private set; }
/// <summary>
/// This is the Y component of this share.
/// </summary>
public BigInteger Y { get; private set; }
/// <summary>
/// This serializes the share into a byte array.
/// </summary>
/// <returns>The serialized contents of this share.</returns>
public byte[] Serialize()
{
try
{
byte[] temp;
MemoryStream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(X);
temp = Y.ToByteArray();
binaryWriter.Write(temp.Length);
binaryWriter.Write(temp);
binaryWriter.Flush();
temp = memoryStream.ToArray();
return temp;
}
catch (Exception)
{
throw new SerializationException("Serialization failed.");
}
}
/// <summary>
/// This outputs the information on this Share.
/// </summary>
/// <returns>A string representing this share.</returns>
public override string ToString()
{
return string.Format("secret({0})={1}", X, Y);
}
}
/// <summary>
/// This is a modified implementation of Shamir's secret sharing.
/// </summary>
public class ShamirEngine
{
/// <summary>
/// This is a SecureRandom item that's used to create the Coefficients, Q, the shares, etc.
/// </summary>
public static readonly SecureRandom random = new SecureRandom();
/// <summary>
/// These are the coefficients used for the polynomial that will be used to create the shares.
/// </summary>
private readonly BigInteger[] Coefficients;
/// <summary>
/// This creates a new ShamirEngine object.
/// </summary>
/// <param name = "k">This is the threshold value. You need at least k shares to get the secret.</param>
/// <param name = "bits">This is how big a secret you want to make. Needs to be in multiples of 8. For example, use 256, and it will create a 256 bit secret that you can use as a key for a cipher.</param>
public ShamirEngine(long k, int bits)
{
if (k < 2)
throw new InvalidParameterException("K needs to be at least 2.");
if (bits%8 != 0)
throw new InvalidParameterException("Bits needs to be a multiple of 8.");
Q = BigInteger.ProbablePrime(bits, random);
Coefficients = new BigInteger[k];
for (int i = 0; i < k; i++)
Coefficients[i] = new BigInteger(bits - 1, random);
}
/// <summary>
/// This creates a ShamirEngine object from serialized data.
/// </summary>
/// <param name = "serialized">This is a byte[] that is filled with the serialized data.</param>
public ShamirEngine(byte[] serialized)
{
try
{
byte[] temp;
MemoryStream memoryStream = new MemoryStream(serialized);
BinaryReader binaryReader = new BinaryReader(memoryStream);
Coefficients = new BigInteger[binaryReader.ReadInt32()];
for (int i = 0; i < Coefficients.Length; i++)
{
temp = new byte[binaryReader.ReadInt32()];
binaryReader.Read(temp, 0, temp.Length);
Coefficients[i] = new BigInteger(temp);
}
temp = new byte[binaryReader.ReadInt32()];
binaryReader.Read(temp, 0, temp.Length);
Q = new BigInteger(temp);
}
catch (Exception)
{
throw new SerializationException("Deserialization failed.");
}
}
/// <summary>
/// This is the threshold level. You need at LEAST this many keys to get the secret / answer.
/// </summary>
public long K { get; private set; }
/// <summary>
/// This is a random prime that's used to make things a bit more secure, and to keep each key's size a bit smaller. Seems to work for some implementations, so why not this one. Besides, it doesn't work without it either. So why not.
/// </summary>
public BigInteger Q { get; private set; }
/// <summary>
/// This Serialize this ShamirEngine object. Keep it's output safe, it is what is used to create new shares.
/// </summary>
/// <returns>The Serialized ShamirEngine object.</returns>
public byte[] Serialize()
{
try
{
byte[] temp;
MemoryStream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(Coefficients.Length);
for (int i = 0; i < Coefficients.Length; i++)
{
temp = Coefficients[i].ToByteArray();
binaryWriter.Write(temp.Length);
binaryWriter.Write(temp);
}
temp = memoryStream.ToArray();
return temp;
}
catch (Exception)
{
throw new SerializationException("Deserialization failed.");
}
}
/// <summary>
/// This puts X through the Coefficients, so that you get Y. It's used to create new shares.
/// </summary>
/// <param name = "X">This is what is multiplied.</param>
/// <returns>The Y part of the share.</returns>
private BigInteger Poly(BigInteger X)
{
BigInteger Y = Coefficients[0];
for (int i = 1; i < Coefficients.Length; i++)
Y = Y.Add(Coefficients[i].Multiply(X.Pow(i)));
return Y.Mod(Q.Subtract(BigInteger.One));
}
/// <summary>
/// This outputs the secret as a byte[] array.
/// </summary>
/// <returns>The secret.</returns>
public byte[] GetSecret()
{
return Coefficients[0].ToByteArray();
}
/// <summary>
/// This creates a new, random Share object.
/// </summary>
/// <returns>The new share.</returns>
public Share GetShare()
{
long x = random.NextLong();
return GetShare(x);
}
/// <summary>
/// This creates a new Share object from the supplied X.
/// </summary>
/// <param name = "x">The X part of the share you want to make. If this is negative, it will automatically make it positive. Note that setting this to 0 will just return the secret.</param>
/// <returns>The new share.</returns>
public Share GetShare(long x)
{
if (x < 0)
x = -x;
BigInteger y = Poly(BigInteger.ValueOf(x));
return new Share(x, y);
}
/// <summary>
/// This returns new, random shares.
/// </summary>
/// <param name = "count">The number of new shares to create.</param>
/// <returns>The new shares, in an array.</returns>
public Share[] GetShares(int count)
{
Share[] Shares = new Share[count];
for (int i = 0; i < count; i++)
{
long x = i + 1; //random.Next(1,256); // random.NextLong(); // testing, wee!
Shares[i] = GetShare(x);
}
return Shares;
}
/// <summary>
/// This combines the shares into the secret again.
/// </summary>
/// <param name = "shares">An array of shares. ONLY include as many shares as you need. If K was 3, and you include 4 shares, it will fail.</param>
/// <param name = "Q">The value of Q used to create the shares.</param>
/// <returns>The secret, as a byte array.</returns>
public static byte[] CombineShares(Share[] shares, BigInteger Q)
{
BigInteger secret = BigInteger.One;
for (int i = 0; i < shares.Length; i++)
{
double lambda = 1;
for (int j = 0; j < shares.Length; j++)
{
if (j != i)
{
lambda = lambda*(((shares[j].X + 1))/((double) (shares[j].X - shares[i].X)));
}
}
secret = secret.Multiply(shares[i].Y.ModPow(BigInteger.ValueOf((long) lambda), Q)).Mod(Q);
}
return secret.ToByteArray();
// The idea behind this, by the way, is you can set it to use 256 bit keys, and Boom! There's a key for most ciphers.
}
}
}[/cpp]
[QUOTE=Chad Mobile;21083100]See, I know [B]how[/B] to help someone.[/QUOTE]
Oh, It's April 1st, almost had me for a second there chad.
[QUOTE=ShaRose_;21089375]So nobody has ANY idea what's wrong with my code either?
[editline]11:44PM[/editline]
Well, I did some cleaning and stuff on the code anyways while waiting for someone to come up with why it doesn't work.
[cpp]using System;
using System.IO;
using System.Runtime.Serialization;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Security;
namespace Shamir
{
/// <summary>
/// This is a share to be used with a ShamirEngine object.
/// </summary>
public class Share
{
/// <summary>
/// This creates a Share from a point made up of a long and a BigInteger.
/// </summary>
/// <param name = "x">The X component.</param>
/// <param name = "y">The Y component.</param>
public Share(long x, BigInteger y)
{
X = x;
Y = y;
}
/// <summary>
/// This creates a share from serialized data.
/// </summary>
/// <param name = "serialized">The serialized data of a share.</param>
public Share(byte[] serialized)
{
try
{
MemoryStream memoryStream = new MemoryStream(serialized);
BinaryReader binaryReader = new BinaryReader(memoryStream);
X = binaryReader.ReadInt64();
byte[] temp = new byte[binaryReader.ReadInt32()];
binaryReader.Read(temp, 0, temp.Length);
Y = new BigInteger(temp);
}
catch (Exception)
{
throw new SerializationException("Deserialization failed.");
}
}
/// <summary>
/// This is the X component of this share.
/// </summary>
public long X { get; private set; }
/// <summary>
/// This is the Y component of this share.
/// </summary>
public BigInteger Y { get; private set; }
/// <summary>
/// This serializes the share into a byte array.
/// </summary>
/// <returns>The serialized contents of this share.</returns>
public byte[] Serialize()
{
try
{
byte[] temp;
MemoryStream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(X);
temp = Y.ToByteArray();
binaryWriter.Write(temp.Length);
binaryWriter.Write(temp);
binaryWriter.Flush();
temp = memoryStream.ToArray();
return temp;
}
catch (Exception)
{
throw new SerializationException("Serialization failed.");
}
}
/// <summary>
/// This outputs the information on this Share.
/// </summary>
/// <returns>A string representing this share.</returns>
public override string ToString()
{
return string.Format("secret({0})={1}", X, Y);
}
}
/// <summary>
/// This is a modified implementation of Shamir's secret sharing.
/// </summary>
public class ShamirEngine
{
/// <summary>
/// This is a SecureRandom item that's used to create the Coefficients, Q, the shares, etc.
/// </summary>
public static readonly SecureRandom random = new SecureRandom();
/// <summary>
/// These are the coefficients used for the polynomial that will be used to create the shares.
/// </summary>
private readonly BigInteger[] Coefficients;
/// <summary>
/// This creates a new ShamirEngine object.
/// </summary>
/// <param name = "k">This is the threshold value. You need at least k shares to get the secret.</param>
/// <param name = "bits">This is how big a secret you want to make. Needs to be in multiples of 8. For example, use 256, and it will create a 256 bit secret that you can use as a key for a cipher.</param>
public ShamirEngine(long k, int bits)
{
if (k < 2)
throw new InvalidParameterException("K needs to be at least 2.");
if (bits%8 != 0)
throw new InvalidParameterException("Bits needs to be a multiple of 8.");
Q = BigInteger.ProbablePrime(bits, random);
Coefficients = new BigInteger[k];
for (int i = 0; i < k; i++)
Coefficients[i] = new BigInteger(bits - 1, random);
}
/// <summary>
/// This creates a ShamirEngine object from serialized data.
/// </summary>
/// <param name = "serialized">This is a byte[] that is filled with the serialized data.</param>
public ShamirEngine(byte[] serialized)
{
try
{
byte[] temp;
MemoryStream memoryStream = new MemoryStream(serialized);
BinaryReader binaryReader = new BinaryReader(memoryStream);
Coefficients = new BigInteger[binaryReader.ReadInt32()];
for (int i = 0; i < Coefficients.Length; i++)
{
temp = new byte[binaryReader.ReadInt32()];
binaryReader.Read(temp, 0, temp.Length);
Coefficients[i] = new BigInteger(temp);
}
temp = new byte[binaryReader.ReadInt32()];
binaryReader.Read(temp, 0, temp.Length);
Q = new BigInteger(temp);
}
catch (Exception)
{
throw new SerializationException("Deserialization failed.");
}
}
/// <summary>
/// This is the threshold level. You need at LEAST this many keys to get the secret / answer.
/// </summary>
public long K { get; private set; }
/// <summary>
/// This is a random prime that's used to make things a bit more secure, and to keep each key's size a bit smaller. Seems to work for some implementations, so why not this one. Besides, it doesn't work without it either. So why not.
/// </summary>
public BigInteger Q { get; private set; }
/// <summary>
/// This Serialize this ShamirEngine object. Keep it's output safe, it is what is used to create new shares.
/// </summary>
/// <returns>The Serialized ShamirEngine object.</returns>
public byte[] Serialize()
{
try
{
byte[] temp;
MemoryStream memoryStream = new MemoryStream();
BinaryWriter binaryWriter = new BinaryWriter(memoryStream);
binaryWriter.Write(Coefficients.Length);
for (int i = 0; i < Coefficients.Length; i++)
{
temp = Coefficients[i].ToByteArray();
binaryWriter.Write(temp.Length);
binaryWriter.Write(temp);
}
temp = memoryStream.ToArray();
return temp;
}
catch (Exception)
{
throw new SerializationException("Deserialization failed.");
}
}
/// <summary>
/// This puts X through the Coefficients, so that you get Y. It's used to create new shares.
/// </summary>
/// <param name = "X">This is what is multiplied.</param>
/// <returns>The Y part of the share.</returns>
private BigInteger Poly(BigInteger X)
{
BigInteger Y = Coefficients[0];
for (int i = 1; i < Coefficients.Length; i++)
Y = Y.Add(Coefficients[i].Multiply(X.Pow(i)));
return Y.Mod(Q.Subtract(BigInteger.One));
}
/// <summary>
/// This outputs the secret as a byte[] array.
/// </summary>
/// <returns>The secret.</returns>
public byte[] GetSecret()
{
return Coefficients[0].ToByteArray();
}
/// <summary>
/// This creates a new, random Share object.
/// </summary>
/// <returns>The new share.</returns>
public Share GetShare()
{
long x = random.NextLong();
return GetShare(x);
}
/// <summary>
/// This creates a new Share object from the supplied X.
/// </summary>
/// <param name = "x">The X part of the share you want to make. If this is negative, it will automatically make it positive. Note that setting this to 0 will just return the secret.</param>
/// <returns>The new share.</returns>
public Share GetShare(long x)
{
if (x < 0)
x = -x;
BigInteger y = Poly(BigInteger.ValueOf(x));
return new Share(x, y);
}
/// <summary>
/// This returns new, random shares.
/// </summary>
/// <param name = "count">The number of new shares to create.</param>
/// <returns>The new shares, in an array.</returns>
public Share[] GetShares(int count)
{
Share[] Shares = new Share[count];
for (int i = 0; i < count; i++)
{
long x = i + 1; //random.Next(1,256); // random.NextLong(); // testing, wee!
Shares[i] = GetShare(x);
}
return Shares;
}
/// <summary>
/// This combines the shares into the secret again.
/// </summary>
/// <param name = "shares">An array of shares. ONLY include as many shares as you need. If K was 3, and you include 4 shares, it will fail.</param>
/// <param name = "Q">The value of Q used to create the shares.</param>
/// <returns>The secret, as a byte array.</returns>
public static byte[] CombineShares(Share[] shares, BigInteger Q)
{
BigInteger secret = BigInteger.One;
for (int i = 0; i < shares.Length; i++)
{
double lambda = 1;
for (int j = 0; j < shares.Length; j++)
{
if (j != i)
{
lambda = lambda*(((shares[j].X + 1))/((double) (shares[j].X - shares[i].X)));
}
}
secret = secret.Multiply(shares[i].Y.ModPow(BigInteger.ValueOf((long) lambda), Q)).Mod(Q);
}
return secret.ToByteArray();
// The idea behind this, by the way, is you can set it to use 256 bit keys, and Boom! There's a key for most ciphers.
}
}
}[/cpp][/QUOTE]
give up and use aes.
[QUOTE=pondefloor;21090040]give up and use aes.[/QUOTE]
[url=http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing]Shamir's secret sharing[/url] is not a cipher. AES isn't applicable as a substitute.
(Secret-sharing algorithms are often used to divide up [i]keys[/i] for ciphers. If it takes coperation between several people to obtain the complete key, there's less likelihood of it used for things it shouldn't.)
[QUOTE=Chad Mobile;21083100]See, I know [B]how[/B] to help someone.[/QUOTE]
You just don't know how to [b]be[/b] helped.
[QUOTE=Wyzard;21090216][url=http://en.wikipedia.org/wiki/Shamir's_Secret_Sharing]Shamir's secret sharing[/url] is not a cipher. AES isn't applicable as a substitute.
(Secret-sharing algorithms are often used to divide up [i]keys[/i] for ciphers. If it takes coperation between several people to obtain the complete key, there's less likelihood of it used for things it shouldn't.)[/QUOTE]
This.
It also could be used for a keygen thing, which is why I'm interested in it. I've ALWAYS wanted to do one, don't know why.
ok, since i still suck at programming, especially c++, why isn't this working, it tells me it won't open/write to the file.
[cpp]
#include <string>
#include <fstream>
#include <iostream>
class FileHandle
{
public:
bool OpenFile(const char * filename)
{
std::fstream file(filename, std::fstream::in | std::fstream::out);
if(file.fail())
{
file.close();
std::cout << "Error opening file " << filename << "\n";
return false;
}
else
{
file.close();
return true;
}
}
bool WriteToFile(const char * name, const char * fileData)
{
std::ofstream file(name, std::fstream::in | std::fstream::out);
file.write(fileData, sizeof(fileData));
file.close();
if(file.fail())
{
std::cout << "Error writing to file " << name << "\n";
return false;
}
else
{
return true;
}
}
};[/cpp]
then the code i use:
[cpp]
#include "file.h"
int main()
{
FileHandle file;
file.OpenFile("test.txt");
file.WriteToFile("test.txt", "weiners");
std::cin.get();
}
[/cpp]
Offtopic but WriteToFile needs to open a file anyway so why not call OpenFile inside there, then you don't have to call it yourself.
true
[QUOTE=layla;21100716]Offtopic but WriteToFile needs to open a file anyway so why not call OpenFile inside there, then you don't have to call it yourself.[/QUOTE]
I'm thinking that the goal would be for file editing without repeatedly opening the file.
Think about it :
When you type something in notepad, do you save it and close it when you're finished writing then reopen it immediately so you can proof-read what you wrote?
[editline]03:06PM[/editline]
[QUOTE=raccoon12;21100154]ok, since i still suck at programming, especially c++, why isn't this working, it tells me it won't open/write to the file.
[cpp]
#include <string>
#include <fstream>
#include <iostream>
class FileHandle
{
public:
bool OpenFile(const char * filename)
{
std::fstream file(filename, std::fstream::in | std::fstream::out);
if(file.fail())
{
file.close();
std::cout << "Error opening file " << filename << "\n";
return false;
}
else
{
file.close();
return true;
}
}
bool WriteToFile(const char * name, const char * fileData)
{
std::ofstream file(name, std::fstream::in | std::fstream::out);
file.write(fileData, sizeof(fileData));
file.close();
if(file.fail())
{
std::cout << "Error writing to file " << name << "\n";
return false;
}
else
{
return true;
}
}
};[/cpp]
then the code i use:
[cpp]
#include "file.h"
int main()
{
FileHandle file;
file.OpenFile("test.txt");
file.WriteToFile("test.txt", "weiners");
std::cin.get();
}
[/cpp][/QUOTE]
Does it give both the errors for opening and writing or just the error for writing?
[editline]03:19PM[/editline]
Nothing is wrong with it, you simply put in an invalid path. Try :
C:\test.txt
and place said file into C:\
I do recommend cleaning up your code, perhaps making the fstream for file a private member of the class, or even just making the path from OpenFile a private member of the class. Just some suggestions, also you should probably reformat it to look more like :
[cpp]
class Class1 {
public:
int member1();
private:
double member2();
}
int Class1::member1() {
// do stuff
}
double Class1:member2() {
// do other stuff
}
[/cpp]
[QUOTE=<ToD> Aaron;21101863]I'm thinking that the goal would be for file editing without repeatedly opening the file.[/QUOTE]
Then just have openfile return if it's already open.
[QUOTE=layla;21102187]Then just have openfile return if it's already open.[/QUOTE]
My point was more that he ends up using OpenFile for no reason other than to check that it exists because it's opened again in WriteToFile.
[editline]03:25PM[/editline]
Sorry, reread what you wrote originally. You're correct.
I still suggest saving the fstream element to the class so that OpenFile has a purpose.
[QUOTE=<ToD> Aaron;21101863]I'm thinking that the goal would be for file editing without repeatedly opening the file.
Think about it :
When you type something in notepad, do you save it and close it when you're finished writing then reopen it immediately so you can proof-read what you wrote?
[editline]03:06PM[/editline]
Does it give both the errors for opening and writing or just the error for writing?
[editline]03:19PM[/editline]
Nothing is wrong with it, you simply put in an invalid path. Try :
C:\test.txt
and place said file into C:\
I do recommend cleaning up your code, perhaps making the fstream for file a private member of the class, or even just making the path from OpenFile a private member of the class. Just some suggestions, also you should probably reformat it to look more like :
class Class1 {
public:
int member1();
private:
double member2();
}
int Class1::member1() {
// do stuff
}
double Class1:member2() {
// do other stuff
}
[/QUOTE]
Thanks, but it still says it can't write/open the file
[QUOTE=raccoon12;21102279]Thanks, but it still says it can't write/open the file[/QUOTE]
I tested it myself. You're gonna have to give an absolute path since I'm pretty sure you're running it out of your compiler which should put it in an obscure location. C:\test.txt was an example, just put the absolute path to the file in there (C:\Users\raccoon12\Desktop\test.txt for example if you're on Windows 7, you're username is raccoon12, and it's on your desktop)
[editline]03:31PM[/editline]
By any chance are you migrating from C?
I heard someone say somewhere that C users migrating to C++ often use lots of pointers, and I just noticed you do that.
There's really no reason to use const char * for everything.
[editline]03:32PM[/editline]
Your write function was being weird for me, it only wrong "wein" which probably has something to do with the above or that sizeof call.
[QUOTE=raccoon12;21100154]ok, since i still suck at programming, especially c++, why isn't this working, it tells me it won't open/write to the file.
[/QUOTE]
Your code seems to be very mixed up. Not sure what you are trying to achieve here.
[cpp]
bool OpenFile(const char * filename)
{
std::fstream file(filename, std::fstream::in | std::fstream::out);
if(file.fail())
{
file.close();
std::cout << "Error opening file " << filename << "\n";
return false;
}
else
{
file.close();
return true;
}
}
[/cpp]
What are you trying to make the above method do?, The name OpenFile and FileHandle suggests your class is going to allow you to tell filehandle to open a file, perform some writing/reading operations on it, then close the file. If that is what you are trying to do, the fstream should be a member variable, and you would be opening it once, and closing it once.
However your OpenFile method as it stands, opens a file, and closes it and returns true of false, depending on if the file could be opened. If that's what you mean it to do, is bool CanFileBeOpened(..), is more appropriate?
Other tips, file.is_open(), can be used to test if a file was opened sucessfuly. There is then no need to close the file, since it could not be opened.
Your write to file method:
[cpp]
bool WriteToFile(const char * name, const char * fileData)
{
std::ofstream file(name, std::fstream::in | std::fstream::out);
file.write(fileData, sizeof(fileData));
file.close();
if(file.fail())
{
std::cout << "Error writing to file " << name << "\n";
return false;
}
else
{
return true;
}
}
[/cpp]
You are taking in a char* for the data. And using sizeof(fileData). sizeof(fileData) is returning the size of the pointer, not the size of the buffer. You need to add an argument to the method that provides the size of the buffer if you are going to use buffers like this. However I'd suggest you use an std::string if your intention is to output text.
I'm not even sure what OpenFile is trying to do, you're opening and closing it then in WriteToFile you're opening it again, making OpenFile pointless.
[QUOTE=<ToD> Aaron;21102339]I tested it myself. You're gonna have to give an absolute path since I'm pretty sure you're running it out of your compiler which should put it in an obscure location. C:\test.txt was an example, just put the absolute path to the file in there (C:\Users\raccoon12\Desktop\test.txt for example if you're on Windows 7, you're username is raccoon12, and it's on your desktop)[/QUOTE]
[cpp]
#include "file.h"
int main()
{
FileHandle file;
file.WriteToFile("C:\\test.txt", "weiners");
std::cin.get();
}
[/cpp]removed OpenFile
Still doesn't work =(
edit:
ok hold on gotta read all this help
[QUOTE=bean_xp;21102399]Your code seems to be very mixed up. Not sure what you are trying to achieve here.
*CODE*
What are you trying to make the above method do?, The name OpenFile and FileHandle suggests your class is going to allow you to tell filehandle to open a file, perform some writing/reading operations on it, then close the file. If that is what you are trying to do, the fstream should be a member variable, and you would be opening it once, and closing it once.
However your OpenFile method as it stands, opens a file, and closes it and returns true of false, depending on if the file could be opened. If that's what you mean it to do, is bool CanFileBeOpened(..), is more appropriate?
Other tips, file.is_open(), can be used to test if a file was opened sucessfuly. There is then no need to close the file, since it could not be opened.
Your write to file method:
*CODE*
You are taking in a char* for the data. And using sizeof(fileData). sizeof(fileData) is returning the size of the pointer, not the size of the buffer. You need to add an argument to the method that provides the size of the buffer if you are going to use buffers like this. However I'd suggest you use an std::string if your intention is to output text.[/QUOTE]
No offence but you just repeated everything I told him in a wall of text.
[QUOTE=<ToD> Aaron;21102478]No offence but you just repeated everything I told him in a wall of text.[/QUOTE]
No you didn't? Before you snipped whatever post. you pretty much said everything is working correctly and his filepath is wrong :raise:
[code]TextWriter tw = new StreamWriter("text.txt");[/code]
Wassap y'all.
[QUOTE=raccoon12;21102425][cpp]
#include "file.h"
int main()
{
FileHandle file;
file.WriteToFile("C:\\test.txt", "weiners");
std::cin.get();
}
[/cpp]removed OpenFile
Still doesn't work =(
edit:
ok hold on gotta read all this help[/QUOTE]
start by changing everything from const char * to string (#include <string>)
now your WriteToFile function will work.
next, reformat the code as I suggested.
that will make the code easier to work with.
Repost it when you're done.
[editline]03:39PM[/editline]
[QUOTE=layla;21102516]No you didn't? Before you snipped whatever post. you pretty much said everything is working correctly and his filepath is wrong :raise:[/QUOTE]
I know, then I noticed some other stuff, edited the post accordingly. He still repeated what I said.
[QUOTE=raccoon12;21102425]
ok hold on gotta read all this help[/QUOTE]
I suggest reading bean_xp's post and reading through [url]http://www.cplusplus.com/doc/tutorial/files/[/url]
[QUOTE=<ToD> Aaron;21102551]start by changing everything from const char * to string (#include <string>)
now your WriteToFile function will work.
next, reformat the code as I suggested.
that will make the code easier to work with.
Repost it when you're done.[/QUOTE]
-snip-
string.c_str()
[QUOTE=raccoon12;21102586]you have to use char * for fstream functions[/QUOTE]
std::string, has a .c_str() method which returns the char* corresponding to the contents of the string, which you can use with fstreams.
is this better?
[cpp]
bool WriteToFile(std::string name, std::string fileData)
{
std::fstream file(name.c_str(), std::fstream::in | std::fstream::out);
file << fileData;
if(file.fail())
{
std::cout << "Error writing to file " << name << "\n";
return false;
}
else
{
return true;
}
file.close();
}
[/cpp]
still doesn't work though =(
[editline]02:42PM[/editline]
[QUOTE=bean_xp;21102610]std::string, has a .c_str() method which returns the char* corresponding to the contents of the string, which you can use with fstreams.[/QUOTE]
yeah, remembered that right as i posted that
I think you can just use ofstream seeing as you're just writing to.
just realized, my file will never close
edit:
my function ends before closing it
[editline]02:48PM[/editline]
[cpp]
void WriteToFile(std::string name, std::string fileData)
{
std::ofstream file(name.c_str(), std::fstream::in | std::fstream::out);
file << fileData;
if(file.is_open())
{
std::cout << "File is open..";
}
else
{
std::cout << "File isn't open...";
}
file.close();
}
[/cpp]
Says file isn't open
Sorry, you need to Log In to post a reply to this thread.