Click here to Skip to main content
15,896,013 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to convert this code into C# -:
C#
fp = fopen("Book1.csv", "w");
                 if (fp == null)
                 {
                     Console.Write("Coudn't open file\n");
                     //      return;
                 }
                 int g;
                 fprintf(fp, "%s,%s,%s\n", ("value of w"), ("Pa"), ("Pb"));
                 for (g = 0; g <= p2; g++)
                 {
                     fprintf(fp, "%d,%f,%f\n", g, Pa[g], Pb[g]);
                 }
Posted
Updated 17-Jul-12 0:41am
v2

C#
try{
using(StreamWriter sw=new StreamWriter("Book1.csv"))
{
 /*fprintf(fp, "%s,%s,%s\n", ("value of w"), ("Pa"), ("Pb"));
                   for (g = 0; g <= p2; g++)
                   {
                       fprintf(fp, "%d,%f,%f\n", g, Pa[g], Pb[g]);
                   }*/

sw.WriteLine(string.Format("{0},{1},{2}","value of w","Pa","Pb"));

for (g = 0; g <= p2; g++)
{
  sw.WriteLine(string.Format("{0},{1},{2}",g,Pa[g],Pb[g]));
}

sw.Flush();  
}
}
catch(IOException){Console.Write("Coudnt open file\n");
}
 
Share this answer
 
Comments
Kenneth Haugland 17-Jul-12 7:15am    
Arent you witing to a file instead of reading one here?
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^]
Oleksandr Kulchytskyi 17-Jul-12 7:22am    
Yep, sorry for my incorrectness, i'm really writing to file =)
Kenneth Haugland 17-Jul-12 7:25am    
Hmm.... actually I think I got it worng... Sorry .....
You can use String.Format for that. Check out msdn:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^]

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900