Hi Guys,

I have a file which contains the following info (it realtes to CPU usage):

1 0 2 2
0 10 1 11

It is stored in a text file. I need to read each line in turn into an array and then import it into a sql database. I know how to read a line into an array but i want each number to be seperate elements in the array. So the code would read line 1 (1 0 2 2) and create an array with four seperate values do something ething else then go back and read the second line.

I know how many lines are in the file by looking up some info in my database (NoDisks) as there could be up to 30 cpus on one system so the code i have so far is this:

for (int i = 1; i<= NoDisks;i++)
                            {
                            StreamReader sr = new StreamReader(fileName.FullName);
                            ArrayList arrText3 = new ArrayList();
                             string line =   sr.ReadLine();
                            arrText3.Add(fileName.LastWriteTime);
                            importCPU(serverID, arrText3, dataConnection);
                            }

it only reads the line into the array one at a time i think i need to get it read the line and then seperate via the spaces but i cant work it out. Any help?

Recommended Answers

All 5 Replies

string[] allData = YourMethodWhichReadFromFile();
foreach (string line in allData)
string[] lineItems = line.Split(' ');
//insert lineItem to database

hi,

im getting errors heres the code:

for (int i = 1; i<= NoDisks;i++)
                            {
                            StreamReader sr = new StreamReader(fileName.FullName);
                            string[] allData = sr.ReadLine();
                                foreach (string line in allData)
                                    string[] lineItems = line.Split(' ');
//insert to database here
}

errors are

Error	1	Embedded statement cannot be a declaration or labeled statement	C:\Users\chris\Documents\work\uni\year3\project\visualproject\ConsoleApplication1\ConsoleApplication1\reports.cs	289	37	ConsoleApplication1
Error	2	Cannot implicitly convert type 'string' to 'string[]'	C:\Users\chris\Documents\work\uni\year3\project\visualproject\ConsoleApplication1\ConsoleApplication1\reports.cs	287	48	ConsoleApplication1

please help

error 2 is caused by

string[] allData = sr.ReadLine();

Read what it says. It tells you what the ReadLine returns, and whats wrong with your line.

The first error is to do with your second error

And the first too, I didn't mean to use my code as it's!

sorry should have marked this as solved, i managed to fix i was just being silly thanks for the help though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.