1

Hello guys I'm writing a test file for a program. where all the possible numbers are tested and i want the result to be logged as a .csv file,so i can upload it into excel.

float calc (float i, float j , float p, float ex){

    float nodalatio = (p/ex); 

    float ans = (0.68 *j + 1.22*nodalatio + 0.34*j -0.81);

    return ans;

}  

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   float stage , grade, pos, ex;
    float resul;


    for (int i=1;i<=3;i++){
        stage = i;

        for(int j=1;j<=3;j++){
            grade = j;
            for(int p=1;p<=60;p++){
                pos = p;
                for(int e=1;e<=60;e++){

                    ex=e;
                    resul = calc(stage, grade,pos,ex);
                    NSLog(@"stage is %f grade is %f,pos is %f ex is %f the result is %f",stage,grade,pos,ex,resul);



                }

            }

        }
    }
    [pool drain];
    return 0;
}

the above is the test code and i can't seem to figure how to output this in to a .csv file. does the code in the loop or after the loop. this is what i had but this did nothing !

NSString *file_path = @"test.csv";
NSString *test_1 = [NSString stringwithformat@"%f",resu];
[test_1 writeToFile:file_path atomically:YES encoding:NSUnicodeStringEncoding error:nil]; 

thank you

2 Answers 2

1

Try this:

float calc(float, float, float, float);

float calc (float i, float j , float p, float ex)
{
    float nodalratio = (p / ex); 
    float ans = (0.68 * j + 1.22 * nodalratio + 0.34 * j - 0.81);
    return ans;
}  

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    float stage , grade, pos, ex;
    float resul;

    [[NSFileManager defaultManager] createFileAtPath: @"test.csv" contents: [@"" dataUsingEncoding: NSUnicodeStringEncoding] attributes: nil];
    NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath: @"test.csv"];
    [file seekToEndOfFile];

    for (int i = 1; i <= 3; i++)
    {
        stage = i;
        for(int j = 1; j <= 3; j++)
        {
            grade = j;
            for(int p = 1; p <= 60; p++)
            {
                pos = p;
                for(int e = 1; e <= 60; e++)
                {
                    ex = e;
                    resul = calc(stage, grade, pos, ex);

                    NSString *str = [NSString stringWithFormat: @"%f, %f, %f, %f, %f\n", stage, grade, pos, ex, resul];
                    [file writeData: [str dataUsingEncoding: NSUTF16LittleEndianStringEncoding]];                 
                }
            }
        }
    }

    [file closeFile];

    [pool drain];
    return 0;
}

That works for me. It will contain a proper BOM and write each string in UTF-16 (Unicode). Using other encodings, like NSUTF16StringEncoding, will write a BOM for each line, which is not really what you want.


FWIW, are you sure it is not 0.68 * j and 0.34 * i or vice versa?

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you @Rudy and yes that was a typo in the code. i found that out later when the results where all over the place xD
ohk i tried running the above code. first i made an empty file and named it test.csv and then i ran the code.. but i'm only getting an empty file. there is nothing in it !
Hmmm... remove the file again and find out where the real "test.csv" is located: In Xcode, search the .app in the tree on the left, under Products, and then select Open in Finder from the context menu. "test.csv" is in the same directory. The "test.csv" you created is indeed empty, but that is probably not the same "test.csv" the program wrote. If you want it somewhere else, then specify a directory as well, e.g. "/Users/cybermon/test.csv".
i specified an output this time but the file is still empty.. i deleted the previous file i created. :\
Hmmm... that's weird. Are you sure you are allowed to write to the directory? For me, it works flawlessly. I even specified /Users/velthuis/test.cvs (twice! I #defined the filename and it is used twice, once in createFileAtPath: and once in fileHandleForWritingAtPath:) and it is about 3.3 MB in size.
|
1

Don't reinvent the wheel:

http://github.com/davedelong/CHCSVParser

My CSV parser class also includes a CSV writer for creating CSV files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.