-1

I have 4 .txt documents in a folder, each document with 10 lines of information. I want to save each line from each file in an array. I am looking for a solution that will give me:

//four arrays with 10 variables each

vFile_0[0-9]
vFile_1[0-9]
vFile_2[0-9]
vFile_3[0-9]

I could potentially accomplish this by naming each variable based on i within the loop:

for (int i = 0; i < vCount; i++)
{
    string[] vFileLine_ + i = File.ReadAllLines("document_" + i + ".txt" );
}

This doesn't work, Does anyone know what I am to replace the i with?


EDIT: I am going into further detail. I want to organize Any Number of .txt documents in a folder, that have random amounts of lines inside them.

Example:

Document 1 has 6 lines of information.
Document 2 has 3 lines of information.
Document 3 has 12 lines of information.
Document 4 has 5 lines of information.

I wish to store all that info into arrays, and when done correctly, the variable names will be shown like so:

vFile_0 [5]
vFile_1 [2]
vFile_2 [11]
vFile_3 [4]

As you can see above, each document has a respective variable name with an array amount equal to the lines in that document, each variable in those arrays stores the line of info from that document as a string. The purpose of this type of variable storage is that I could one day run the program and detect 4 files that have 10 lines each, or 120,000 files with 30,000 lines each.

The only issue I am having right now, is naming the variables that way.

  • You can't create variable like that way. Why don't you store all the lines in same array – Rahul Feb 04 '16 at 04:40
  • I can create arrays that way using Autohotkey, are you sure there is no way? – TheBloodSeeker005 Feb 04 '16 at 04:42
  • 2
    NO, you will have to statically declare the variable not dynamically like what you are trying. – Rahul Feb 04 '16 at 04:43
  • http://stackoverflow.com/questions/314008/programatically-using-a-string-as-object-name-when-instantiating-an-object – hkf Feb 04 '16 at 04:49
  • Instead of trying to use separate variables why not place the string arrays into a Dictionary keyed on the file name - Dictionary vFile? Then your access to the file contents would be vFile["document_0"] – richardsonmarkj Feb 04 '16 at 04:51
  • @richardsonmarkj I'm going to need an example to see what you mean – TheBloodSeeker005 Feb 04 '16 at 04:54
  • @TheBloodSeeker005 Effectively the same as you accepted answer - `var vFile = new Dictionary(); for (int i = 0; i < 3; i++) { var name = "document_" + i + ".txt"; vFile[name] = File.ReadAllLines(name); }` – richardsonmarkj Feb 04 '16 at 05:18
  • @richardsonmarkj Will your solution look as clean as my question suggests? Will it end with 4 array variables named in sequence? – TheBloodSeeker005 Feb 04 '16 at 05:40
  • 1
    You *cannot* do this dynamically. Instead of individual variables (and concern over the variable names), you need to use a more appropriate data structure (such as a `List` of `Document` where `Document` is a `class` that contains a name or number, plus the lines contained within the document). – crashmstr Feb 04 '16 at 05:56

3 Answers3

3

You can replace the individual vFile_ arrays with a double array: string[][] vFile, along these lines:

int fileCount = 0; // replace with actual file count...
string [][]vFile = new string[fileCount][];
for (int i = 0; i < fileCount; i++) {
    vFile[i] = File.ReadAllLines("document_" + i + ".txt");
}
object88
  • 720
  • 1
  • 7
  • 20
0

You could use lists? I prefer them over arrays, especially when dealing with strings/files that have a variable number of lines.

List<List<String>> allDocuments = new List<List<String>>(); //Note this is a list of lists
for (int i = 0; i < vCount; i++)
{
    string[] tmpRead = File.ReadAllLines("document_" + i + ".txt" );
    List<String> thisDocument = new List<String>();
    foreach(string line in tmpRead) {
        thisDocument.Add(line);
    }
    allDocuments.Add(thisDocument);
}
Corey Thompson
  • 398
  • 6
  • 18
0

Dictionary could be the best generic option for your problem. Your code would look like below

Dictionary<string, string[]> fileContents = new Dictionary<string, string[]>(); 
for (int i = 0; i < vCount; i++) 
{     
     fileContents[vFileLine_ + i] = File.ReadAllLines("document_" + i + ".txt" ); 
}

This provides generic solution to add contents of as many files as you need.

Chris
  • 113
  • 5