Results 1 to 7 of 7

Thread: Need to read input text and write it to output file

  1. #1
    Join Date
    May 2011
    Posts
    43

    Need to read input text and write it to output file

    Hi all, I need assist please. I read a text file that contains the strings that are unique / duplicate. I have to print the command line in the number of unique entries and write a single file of each word in the input file - each word to be on a separate line. Also I have to print the number of duplicate words and write the words duplicate output file - each entry into a different line. I've managed to open and read the file by using StreamReader ReadLine. Except that it's tough StreamWriter output. At the time that have been printed to the display of every line, but I want it to be written in the output file. How I can do this?

    Code:
    Console::WriteLine("trying to open file {0}...", file);
    // opens text file
    StreamReader ^ inFile = File::OpenText(inputfile);
    StreamWriter ^ outFile = File::CreateText(DupOutputFile);
    String^ str;
    int count = 0;
    // retrieves each line
    while ((str = inFile->ReadLine()) != nullptr)
    {
    count++;
    Console::WriteLine("line {0}: {1}", count, str );

    }
    Is this the most excellent way to keep using StreamReader / Writer? Apparently, you need to write code to recognize DUP and unique words, but I just want to start by writing an output file and the console.

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: Need to read input text and write it to output file

    Like the console has a StreamWriter WriteLine () method. Ok, one is static and the other is an instance method, but other than that is almost identical. It can just replace:
    Code:
    outFile->Write ("line {0}: {1}", count, str);
    with this:
    Code:
    outFile->WriteLine ("line {0}: {1}", count, str);
    I think this will basically perform what you want.

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: Need to read input text and write it to output file

    Well, the term "container" is common in native C + + world, while. NET people prefer to call the thing itself "collection." Essentially both are nothing more than a class (or an instance of that class, ie an object) that can contain a collection of other objects. The simplest (but unfortunately also the most foolish) of containers is a matrix that you probably know.
    The first container in the sentence you cite could well be a SortedSet . When trying to add a string to this collection using its Add () method and the chain is already present in the collection, refuse to add back and indicate that by returning false. So this seems to be ideal for storing strings only: It takes care of their own uniqueness.

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: Need to read input text and write it to output file

    If the SortedSet rejected to add a string you be familiar with which is not unique? This is the time to add to the collection of strings in duplicate so that I, personally, the use of a list. This container has no problem of storing any number of elements equal (which of course does not mean that objects can only store the same).
    Having these two containers, the only thing left to do is to process input strings, one after another and put each one in the container it belongs to the logic that I described above. Once you're done with that (i.e., reach the end of the input file), the only thing left to do is send the collected data as you want.

  5. #5
    Join Date
    Apr 2009
    Posts
    569

    Re: Need to read input text and write it to output file

    Yes As I understand your needs, a parent would not have been a very good choice anyway. Even if you know the number of elements in the input file in advance, we certainly do not know how many of them are unique. If you really know everything that I wonder why you need the program at all. No, already there as part of the structure. NET. At least as long as the items being processed are actually strings. If, however, are of a more complex structure would possibly have to write a class to signify the points, but you could still use the containers supplied by the framework. At least it would not be a need to maintain collections of unique and duplicated.

  6. #6
    Join Date
    May 2009
    Posts
    529

    Re: Need to read input text and write it to output file

    However, there is an alternative between native C + + programmers is a known fact that a set is equivalent to a map where the keys and values are identical. Of course this is in principle valid for C + + / CLI, so you can also use an associative container that is roughly equivalent to native standard:: map, for example Dictionary , Which is available at. NET Framework before 4 and enforces uniqueness of the keys to what is needed here. For values that can offer some dummy value and the value (that is, whatever you want) or just use the same string as the key and the value of each item you might even give you the opportunity to make your program are better from a semantic point of view.

  7. #7
    Join Date
    May 2009
    Posts
    543

    Re: Need to read input text and write it to output file

    Why the hell did you add the input file name as an element in the dictionary? The dictionary is designed to hold the unique sequences to read the file and the list (not shown in the snippet) is to carry out the duplicates. The real work to do on the track at the end of his piece, which is currently not much more than a list of strings to read the console output and file egg.txt
    In the loop you have to do something like this: Read a string from the input file. Try adding the string in the dictionary. If it works, you are finished, continue with the next string. If the above step fails, the string is a copy, so adding to the list. Continue with step 1 until you have processed the entire file.

Similar Threads

  1. Replies: 10
    Last Post: 09-01-2012, 07:48 AM
  2. Read and Write a .txt file with Java
    By Mulan in forum Software Development
    Replies: 4
    Last Post: 28-07-2010, 02:03 AM
  3. How to read 2 text files and write it into one
    By Juaquine in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 06:05 PM
  4. Replies: 5
    Last Post: 02-02-2010, 04:20 PM
  5. File read-write mode in java
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 10:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,693,767.85252 seconds with 17 queries