1

I want to write a string to file, n number of times (n being the length of the string). With each insertion being on a new line.

For example, the string "antallabel" (with a length of 10), should be inserted into a file 10 times, with each insertion on its own line. Should I use an array, list or something else?

I'm really new to programming, so I hope this question makes sense.

Check if file exists, then generate 2 files:

if (File.Exists(fullPath)) {
File.Delete(fullPath);
File.WriteAllText(fullPath, totalv + ";" + laste + ";" + antallabels);  
File.Create(filePath + @"\" + controlFile).Close();  
}  
else  
{  
File.WriteAllText(fullPath, totalv + ";" + laste);
File.Create(filePath + @"\" + controlFile).Close();
JSON C11
  • 11,272
  • 7
  • 78
  • 65

1 Answers1

0

I do not think you need this code at all. The WriteAllText(...) function is explained here.

Creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.

So if it not exist it will be created and if it exists it will be deleted and recreated with your content. Please consider also adding the programming language as tag.

for(int i = 0; i < inputVar.length; i++) { 
  //do sth. as many times as the string is long
  File.AppendAllText(path, inputVar + System.Environment.NewLine);
}
Charlie
  • 1,169
  • 2
  • 16
  • 31
  • 1
    Hello Charlie - Thank you for your reply. "antallabels" is an INT variabel. If "antallabels" is set to, lets say 10, i want the same line in the file 10 times. Hope that makes sense. – Mickey Rasmussen Aug 27 '19 at 13:58