Question Change the contents of the multiple text files

ahmet_koc

New member
Joined
Oct 31, 2017
Messages
4
Programming Experience
Beginner
Hello. There are thousands of text files on hand. There are various data in these files. I need to operate this data in a computer program. But the data in these files is not in the format I want. I want to read the text file contents in order and write a code that will change the contents of the data in the desired data format and save it. I'm glad you helped me.

The format of the ready data:

<number of tasks>
83

<cycle time>
4732

<task times>
1 1673
2 985
3 1836

<precedence relations>
1,2
2,3
2,4

<number of task attributes>
1

<task attribute values>
1,1:70
2,1:6
4,1:16

<attribute bounds per station>
1:100

<linked tasks>
7,11
15,20

<incompatible tasks>
1,81
2,18


Required format:


Nbtasks = 83;

cycleTime = 4732;

taskTime = [1673, 985, 1836];

numberOftaskAttributes = 1;

attributeBounds = {<1,100>};

Precedences = {<1,2>, <2,3>, <2,4>};

taskAttribute ={<1,1,70>, <2,1,60>, <4,1,16>};

Linked = {<7,11>, <15,20>};

Incompatibles = {<1,81>, <2,18>};


Thank you very much for your interest in advance.
 
What have you tried and what happened when you tried it? There's plenty of information out there about reading and writing text files so you should be able to make an attempt at least. Once you can read in one text file, manipulate its contents and then write out the new text, then you can think about implementing some parallelism to speed up the processing of a large number of files.
 
What have you tried and what happened when you tried it? There's plenty of information out there about reading and writing text files so you should be able to make an attempt at least. Once you can read in one text file, manipulate its contents and then write out the new text, then you can think about implementing some parallelism to speed up the processing of a large number of files.

Thank you for your answer. I am a new user for C#. I have not used C# before. My supervisor urgently wants me to write a code to achieve this problem. So I could not try an attempt. You are right, but I do not really have much time. Thank you.
 
We're not a code-writing service. Forums like this are intended to help with specific problems. Maybe someone else will be happy to do your work/assignment for you but I'm afraid I'm not. If you make an attempt and need help with specific issues then we can talk again.
 
Hi. I wrote a code block to change the title of data (for example <number of tasks> was changed to Nbtasks).


            StringBuilder newFile = new StringBuilder();
            string temp = "";
            string[] file = File.ReadAllLines(@"C:\Users\Memo\Desktop\roszieg_c=14.alb");
            foreach (string line in file)
            {
                if (line.Contains("<task times>"))
                {
                    temp = line.Replace(
            "<task times>", "taskTime=");
                    newFile.Append(temp +
            "\r\n");
                    continue;
                }
                newFile.Append(line +
            "\r\n");


            }
            File.WriteAllText(@"C:\Users\Memo\Desktop\roszieg_c=14.alb", newFile.ToString());


The data after these titles must be changed as the required format. But, I could not write a code block to change these data line by line. Do you have a suggestion? Thank you.
 
Given that you need to do something slightly different for each section, I would tend to write a method for each section. When you detect a particular section, call the appropriate method. That method can then read and validate the subsequent data and transform it appropriately. I would probably put all the lines into a List<string> and modify that at each stage, removing and modifying lines as required. When you're done, you can call File.WriteAllLines to write the final output directly from that list and automatically add the line breaks.
 
Here's something to get you started:
private void ProcessFile(string filePath)
{
    var lines = File.ReadAllLines(filePath).ToList();
    var lineIndex = 0;

    while (lineIndex < lines.Count)
    {
        var line = lines[lineIndex];

        switch (line)
        {
            case "<number of tasks>":
                ProcessNumberOfTasks(lines, lineIndex);
                break;
        }

        // Step over the blank line between sections.
        lineIndex += 2;
    }

    File.WriteAllLines(filePath, lines);
}

private void ProcessNumberOfTasks(List<string> lines, int lineIndex)
{
    var numberLine = lines[lineIndex + 1];
    var blankLine = lines[lineIndex + 2];
    int number;

    // Validate section and process.
    if (int.TryParse(numberLine, out number) && blankLine == string.Empty)
    {
        // Rewrite the section header line.
        lines[lineIndex] = $"Nbtasks = {number}";

        // Remove the original number line.
        lines.RemoveAt(lineIndex + 1);
    }
}
 
Back
Top Bottom