-
January 23rd, 2013, 06:38 PM
#1
Writing Structure to CSV file - won't work... HELP!
I need to compute some values, place them in a structure containing two vector variables, and save them in a CSV file. Right now, I can't even generate a file. I wrote the structure definition and file writing code in a header file (after first borrowing it). I have a "button" that is supposed to trigger all of the activity. I'll attach the code below:
HEADER
________-----_______------
#ifndef EINSTEIN_H
#define EINSTEIN_H
#include <stdio.h>
struct SizeAll{
int year;
double R;
};
int write_to_file(int count, struct SizeAll *data, char const *fileName)
{
FILE *f = fopen(fileName, "w");
if (f == NULL) return -1;
while (count-- > 0) {
// you might want to check for out-of-disk-space here, too
fprintf(f, "%d,%d", data->year, data->R);
++data;
}
fclose(f);
return 0;
}
#endif
BUTTON CODE
_____-------______-------
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
double R;
int counter,year,retval;
struct SizeAll Universe;
char const NameFile[62]="D:\\Documents and Settings\\Jumbo\\Desktop\\Friedout.csv";
Universe.R = 1;
Universe.year = 2;
for ( counter = 1 ; counter < 2 ; counter++ )
R = R + 1;
if (counter ==1)
retval=write_to_file(1, &Universe, NameFile);
}
};
}
Can anyone help me to get this to work? My grasp of pointers and how and when to use them evaporated long ago.
Thank you!
Last edited by Protocol; January 31st, 2013 at 11:18 AM.
-
January 23rd, 2013, 11:40 PM
#2
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
I need to compute some values, place them in a structure containing two vector variables, and save them in a CSV file. Right now, I can't even generate a file. I wrote the structure definition and file writing code in a header file (after first borrowing it). I have a "button" that is supposed to trigger all of the activity. I'll attach the code below:
1) Use code tags when posting code. The code you posted is unformatted and practically unreadable.
2)
Code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
This code is not C++. This is Managed C++, which is off-topic for this forum. As far as this forum is concerned, this line is gibberish. There is a Managed C++ forum dedicated to this weird syntax.
3)
Code:
char const NameFile[62]="D:\\Documents and Settings\\Del Ventruella\\Desktop\\Friedout.csv";
So you actually counted the number of characters and came up with 62? What if you're off by one? Next time, do this:
Code:
char const NameFile[]="D:\\Documents and Settings\\Del Ventruella\\Desktop\\Friedout.csv";
Then there is no need to sit at a keyboard hitting the right arrow key x times and counting under your breath, hoping that you aren't distracted by your roommate, your dog, or a phone ringing.
Code:
for ( counter = 1 ; counter < 2 ; counter++ )
R = R + 1;
You do know that this loop only goes once. So why write a loop?
Code:
int write_to_file(int count, struct SizeAll *data, const char *fileName)
There is no need for the struct keyword here for C++.
Code:
int write_to_file(int count, SizeAll *data, const char *fileName)
{
FILE *f = fopen(fileName, "w");
if (f == NULL)
return -1;
while (count-- > 0)
{
fprintf(f, "%d,%d", data->year, data->R);
++data;
}
fclose(f);
return 0;
}
So what is wrong with this function? I don't see any glaring problems with it, given you are calling it with valid data in those parameters.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; January 23rd, 2013 at 11:43 PM.
-
January 24th, 2013, 04:45 AM
#3
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
...
Code:
struct SizeAll{
int year;
double R;
};
int write_to_file(int count, struct SizeAll *data, char const *fileName)
{
FILE *f = fopen(fileName, "w");
if (f == NULL) return -1;
while (count-- > 0) {
// you might want to check for out-of-disk-space here, too
fprintf(f, "%d,%d", data->year, data->R);
++data;
}
fclose(f);
return 0;
}
Why do you format the double R as integer? Does it make any real sense in your case?
Victor Nijegorodov
-
January 24th, 2013, 09:12 AM
#4
Re: Writing Structure to CSV file - won't work... HELP!
1. "Code tags" - I'll try to remember that, if I can get them to work with Chrome.
2. Managed C++ is what VC++ 10 is spitting out in this case, and this is the VC++ forum.
3. No, I didn't count. I originally used the syntax you are recommending. The compiler puked and gave me either a warning or an error, listing the proper number of characters, which I then inserted.
4. I wrote the loop, because in the "final product", I'll need a loop. This is just crude, initial test code. Why waste loop time in a test of a file writing routine?
5. I'll strike the "struct" keyword and see if the software will produce the desired file. (Probably not the most likely error.)
AND
6. Formatting as integer... Does it make sense? I'm computing in billions of year increments. If it spits the results out in whole years with fractional years truncated, I didn't perceive that as a likely problem. I'll happily embrace any insights that offer an alternative approach or better reasoning.
Important: Right now this code will not write the file, and I don't understand why. I tried a Windows search for the file name after I ran this routine, and it found nothing anywhere on any drive on my computer.
Thank you! (This is normally where I go to get the answer that solves the stranger problems I encounter.)
-
January 24th, 2013, 09:19 AM
#5
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
2. Managed C++ is what VC++ 10 is spitting out in this case, and this is the VC++ forum.
You can take that attitude if you like, or you can listen to the people that have been here for years and tell you that if you're using .Net, this is the wrong forum. There is a forum especially for Managed C++, and that's where you're likely to get more help if you're writing .Net apps.
-
January 24th, 2013, 09:20 AM
#6
Re: Writing Structure to CSV file - won't work... HELP!
1. Please, read http://forums.codeguru.com/announcement.php?f=9
2. Please, read the description of this "VC++ forum":
Forum: Visual C++ Programming
Ask questions about Windows programming with Visual C++ and help others by answering their questions.
It has nothing to do with a managed C++, and there is another Forum for a managed C++:
Victor Nijegorodov
-
January 24th, 2013, 09:28 AM
#7
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
6. Formatting as integer... Does it make sense? I'm computing in billions of year increments. If it spits the results out in whole years with fractional years truncated, I didn't perceive that as a likely problem. I'll happily embrace any insights that offer an alternative approach or better reasoning.
Just try this code:
Code:
int main()
{
double d = 897654446.232323232;
printf("%f\n", d); // print as float
printf("%d\n", d); // print as integer
return 0;
}
Do you see some difference in output?
Victor Nijegorodov
-
January 24th, 2013, 10:54 AM
#8
Re: Writing Structure to CSV file - won't work... HELP!
From the preceding announcement:
"Posting
Post to only one forum - post to the forum that fits your question. Posting to more than one forum is considered SPAM and is prohibited without permission"
It seems that according to the "lawyers" I can't shift this to the other forum, at risk of being declared a "SPAMMER".
-
January 24th, 2013, 10:55 AM
#9
Re: Writing Structure to CSV file - won't work... HELP!
VictorN: I have no idea how to write a console program that will run in a DOS window with VC++, or I'd try your "experiment". I'd love to know how the outputs would vary. Thank you.
-
January 24th, 2013, 10:58 AM
#10
Re: Writing Structure to CSV file - won't work... HELP!
P.S. This mess still won't generate a file.
-
January 24th, 2013, 11:05 AM
#11
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
From the preceding announcement:
"Posting
Post to only one forum - post to the forum that fits your question. Posting to more than one forum is considered SPAM and is prohibited without permission"
It seems that according to the "lawyers" I can't shift this to the other forum, at risk of being declared a "SPAMMER".
What's with the attitude? That won't help you get help. Just ask a mod to move it for you.
-
January 24th, 2013, 11:07 AM
#12
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
VictorN: I have no idea how to write a console program that will run in a DOS window with VC++, or I'd try your "experiment". I'd love to know how the outputs would vary. Thank you.
http://msdn.microsoft.com/en-us/vstudio/cc296427.aspx
http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
Victor Nijegorodov
-
January 24th, 2013, 11:17 AM
#13
Re: Writing Structure to CSV file - won't work... HELP!
It appears to include to exclude the decimal portion. Since I don't require the decimal portion, the integer representation is acceptable. Thanks for putting the decimal representation before me. I'd long since forgotten how to represent it.
At this point, I'll accept VC++ as VC++, including any code that it can generate. I'll leave those who assert "attitude" to reflect upon their own.
-
January 24th, 2013, 11:24 AM
#14
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
It appears to include to exclude the decimal portion. Since I don't require the decimal portion, the integer representation is acceptable. Thanks for putting the decimal representation before me. I'd long since forgotten how to represent it.
At this point, I'll accept VC++ as VC++, including any code that it can generate. I'll leave those who assert "attitude" to reflect upon their own.
Posting in the appropriate forum is for your benefit, not mine. If you're working with a particular technology, wouldn't it make more sense to post in the forum that actually discusses that technology?
-
January 24th, 2013, 11:27 AM
#15
Re: Writing Structure to CSV file - won't work... HELP!
Originally Posted by Protocol
It appears to include to exclude the decimal portion. Since I don't require the decimal portion, the integer representation is acceptable.
Wrong!
It interprets the content as an integer including the exponent bits! (See http://en.wikipedia.org/wiki/Double-...g-point_format )So the your representation "as integer" will be wrong!
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|