I would like to write two variables to a file from two for loops like writing a two dimensional array to file. so if I have a two dimension array 3 by 3 then if I was two write it to a file then it would look somethiing like this.
1,1
1,2
1,3
2,1
2,2
2,3
3,1
3,2
3,3
I have got the code below working sort of right I just need to combine the results of both loop into one writer.WriteLine nothing I try seems to work.
string filePath = @"C:\\seating plan one.txt";
using (StreamWriter writer = new StreamWriter(filePath, true))
for (int i = 0; i < 100; i++)
{
writer.WriteLine("i = " + i);
for (int j = 0; j < 10; j++)
{
writer.WriteLine("j = " + j);
}
}
so results from i and j together writer.WriteLine(i, j);
I have tried lots of different file.* but not seems to resolve problem. Can any one help thanks in advance.
Loading
Afzal PawaskarPosted Nov 20, 2010, 3:01 PM
string filePath = @"C:\\seating plan one.txt";
First of all if you use the "@" symbol, there is no need to use"\\"
This is what you should write:
string filePath = @"C:\seating plan one.txt";
And I think this is what you want...
using (StreamWriter writer = new StreamWriter(filePath, true))
for (int i = 0; i < 100; i++)
{
//writer.WriteLine("i = " + i); //no need to use this here
for (int j = 0; j < 10; j++)
{
writer.WriteLine(i+","+ j);//concatenate your strings and write it to your file(as per your sample requirement)
}
}
Carl PotterPosted Nov 20, 2010, 4:08 PM
Zoran HorvatPosted Nov 20, 2010, 2:37 PM
- Which operation system do you use?
- Did you try to create file in some other directory, rather than C root, as writing to C root may be forbidden.
When posting a question, please provide full information about what you see wrong. First of all, if there is exception, provide full exception text including call stack and inner exceptions (Exception.ToString()).