Writing Structure to CSV file - won't work... HELP!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Writing Structure to CSV file - won't work... HELP!

  1. #1
    Join Date
    Feb 2005
    Posts
    34

    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 10:18 AM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,434

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    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 10:43 PM.

  3. #3
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,481

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    ...
    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

  4. #4
    Join Date
    Feb 2005
    Posts
    34

    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.)

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,133

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    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.

  6. #6
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,481

    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++:
    Managed C++ and C++/CLI
    Discuss Managed C++ and .NET-specific questions related to C++.
    Victor Nijegorodov

  7. #7
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,481

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    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

  8. #8
    Join Date
    Feb 2005
    Posts
    34

    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".

  9. #9
    Join Date
    Feb 2005
    Posts
    34

    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.

  10. #10
    Join Date
    Feb 2005
    Posts
    34

    Re: Writing Structure to CSV file - won't work... HELP!

    P.S. This mess still won't generate a file.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,133

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    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.

  12. #12
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,481

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    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

  13. #13
    Join Date
    Feb 2005
    Posts
    34

    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.

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,133

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    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?

  15. #15
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,481

    Re: Writing Structure to CSV file - won't work... HELP!

    Quote Originally Posted by Protocol View Post
    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

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center