#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0

    How to read text file and access them C#


    Hi guys, would appreciate some help here.
    I need some advice and help for my program, "Entrance Exam Analyzer".

    The program should accept entrance exam results of a student (as text file). The text file contains the following:

    - Name of applicant (student)
    - Program applied
    - Answers in a twenty-five question test:
    - 25 items in Math
    - 25 items in English

    So basically a sample text file that my program should read would look like this:

    Code:
    Name of applicant: Josh Nguyen
    Program applied: BS Computer Science
    
    Answers:
    
    Math
    
    1.  A
    2.  C
    3.  B
    4.  B
    5.  A
    6.  D
    7.  A
    8.  A
    9.  A
    10. C
    11. A
    12. D
    13. B
    14. A
    15. B
    16. B
    17. C
    18. A
    19. D
    20. C
    21. B
    22. A
    23. C
    24. C
    25. D
    
    English
    
    1.  A
    2.  C
    3.  B
    4.  B
    5.  A
    6.  D
    7.  A
    8.  A
    9.  A
    10. C
    11. A
    12. D
    13. B
    14. A
    15. B
    16. B
    17. C
    18. A
    19. D
    20. C
    21. B
    22. A
    23. C
    24. C
    25. D
    With the information from the text file, my program should be able to show data as follows:

    - Average score of all applicants who have taken the exam per course
    - Highest/lowest scores per course
    - Item difficulty (the percentage of students that correctly - answered an item)

    Basically, the program can accept a maximum 10 exam results (10 text files).

    I will provide the exam answer key (The correct answers). I should be able to compare the answers i received from the text file from my answer key.

    My question is though, How can i get the answers (only the A,B,C,D) from the text file and save it to an array or something? Because i was planning on saving the answers on an array and compare them to my exam answer key also saved to an array. is this a bad solution for my problem?

    Would really appreciate any kind of advice/help! Thanks!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,215
    Rep Power
    2222
    You open the text file, read in the values, and save them in an array.

    What part of that are you unable to do? If you don't know how to open and read a text file in C#, then you should review the file I/O chapter of your textbook.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    You would use the Streamreader class to implement a text file TextReader. The ReadLine method will be used to read one input line at a time.

    Your input file processing will first check for either the Math or English literal. Once one of these literals are found, you would then read both the number index and the alpha grade into a string list. You can do this as follows:

    Code:
     listStrLineElements = line.Split(' ').ToList();
    where line is the input line read in by ReadLine and listStrLineElements is your string List.

    Index Zero of listStrLineElements would be the actual index number. For example, 1. .. 25. and index One would be the actual alpha grade. Thus, you would load index one into either the Math or English array.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    ...is this a bad solution for my problem?
    Well it's a partial design, not a solution. There's obviously a lot more to it than parsing some files into a couple of arrays. You'll get better assistance with your homework if you demonstrate that you are actually working the problem. Why don't you start with a more complete high level description of what you plan to do, show us the code you have so far, tell us what problems you've run into, then ask some targeted questions.

    There's a couple of ways to approach the line parsing problem. The .NET libraries provide a lot of support in this area (see BobS0327's post), but you could also write a low level loop that iterates over the elements in the line until you get to the information you are interested in.

    Show us how serious you are about doing your own homework and we'd be real happy to help you over the tough spots.
    I no longer wish to be associated with this site.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Hi thanks for all the comment, sorry if my reply is late. i'm a working student and i'm have 2 jobs so i can pay my tuition. again sorry for the late reply.

    I've been working on it, i'm also a big newbie when it comes to coding. Here is what i've got so far.

    Code:
    using System;
    using System.IO;
    
    
    namespace MachineProblemIT111P
    {
        class Program
        {
            static void Main()
            {
    
                string[] lines = File.ReadAllLines(@"C:\Users\Edgar\Documents\Visual Studio 2012\Projects\MachineProblemIT111P\ExamApplicant.txt");
                string tempAnswerMath = "";
                string[] answerKeyForMath = new string[25] { "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A", "B", "C", "D", "A" };
                int correctAnswers = 0;
    
    
                for (int i = 7; i < 32; i++)
                {
                    tempAnswerMath = lines[i];
                    string[] myArray = new string[tempAnswerMath.Length];
                   
                    for (int j = 0; j < tempAnswerMath.Length; j++)
                    {
                        myArray[j] = tempAnswerMath[j].ToString();
                        
                    }
                    Console.Write(myArray[4] + " ");
    
                }
                Console.Write("\nCorrect Answers for Applicant: {0}", correctAnswers);
                System.Console.ReadLine();
    
            }
        }
    }
    I only put the "Console.Write(myArray[4] + " ");" so that i can see if i got the letters correct from the text file. I don't really need to show it though.

    I managed to get the answers for the Math Subject that i want to compare, however when i try to make an IF statement to compare the answers that i got to my "answerKeyForMath" i get weird numbers. Can anyone lend me a hand for a proper FOR/IF statement to compare the two so i can put a value to my "correctAnswers" ?

    I'm very sorry if my code is kinda jumbled or something or not easy to read for you guys. Any suggestions or advice to make the code better is greatly appreciated.

    Thanks.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Code:
    tempAnswerMath = lines[i];
                    string[] myArray = new string[tempAnswerMath.Length];
                   
                    for (int j = 0; j < tempAnswerMath.Length; j++)
                    {
                        myArray[j] = tempAnswerMath[j].ToString();
                        
                    }
                    Console.Write(myArray[4] + " ");
    There is an issue with Console.Write(myArray[4] + " "); It will generate an out of bounds condition since you are referencing the 4th position in myArray. The tempAnswerMath.Length is set at 4. Thus myArray will have 4 positions which are positions Zero thru Three. Using position 4 will generate an index out of bounds condition. I think you want to use myArray[3]
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Hi there bob, i checked my tempAnswerMath.Length, and i can say the length is 5, so it'll have an index of 0,1,2,3,4 right? and the letters are at index 4. So i think i got it right or so.

    my problem is when i try to compare each letter to my answer key so i can count my correctAnswers. :(
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    i checked my tempAnswerMath.Length, and i can say the length is 5
    That would only be correct if the left hand index next to the grade is greater than 9. If it is less than 10, the length will be 4.

    Consider something like this...

    Code:
                    try
                    {
                        Console.Write(  myArray[tempAnswerMath.Length - 1] +" ");
                            
                    }
                     catch (IndexOutOfRangeException)  
                    {
                        Console.WriteLine("Index out-of-bounds!"); 
                    }
    To determine how many correct answers, you would compare myArray[tempAnswerMath.Length -1] to answerKeyForMath[i-7]. You would use i-7 because i is initally set to 7 but you want to start at the zero index in your answerKeyForMath
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Hi there again bob, thanks for the reply. I added a space to numbers 1-9 so they'll have the same number of index with numbers 10-25. I also followed your advice on how to compare. Thanks so much bob.

    I would like to ask a question though, I need to process 10 text files simultaneously (and also get the number of correct answers for each one) that has the same format like the one i put in the thread, is there a way or any kind of loop that i can use so that i can process those 10 text files?
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    I would like to ask a question though, I need to process 10 text files simultaneously (and also get the number of correct answers for each one) that has the same format like the one i put in the thread, is there a way or any kind of loop that i can use so that i can process those 10 text files?
    One possible option, assuming that your 10 files are in one directory and they all have a common extension is

    Code:
    string[] txtFiles = Directory.GetFiles(@"c:\Temp\", "*.txt");
    Iterate thru the txtFiles string array to process each file.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    7
    Rep Power
    0
    Code:
    string[] txtFiles = Directory.GetFiles(@"c:\Temp\", "*.txt");
    Does it mean i should put all the text files inside the ( ) ? If yes, then the content of the 10 text files will all be inside the array?


    Uh. i don't really understand what you mean. can you give an example of the code you gave that already process 10 text files?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    187
    Rep Power
    82
    Assuming that your ten files are all located in the c:\temp folder and that they all have an extension of txt...

    Code:
    using System;
    using System.IO;
    
    namespace MachineProblemIT111P
    {
    class Program
    {
        static void Main()
        {
            string[] txtFiles = Directory.GetFiles(@"c:\Temp\", "*.txt");
            foreach (string s in txtFiles)
            {
                Console.WriteLine(s);
            }
        }
    }
    }

IMN logo majestic logo threadwatch logo seochat tools logo