Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,222 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,210 people online right now. Registration is fast and FREE... Join Now!




Reading and outputting .txt files

 
Reply to this topicStart new topic

Reading and outputting .txt files

Irishancest
1 Dec, 2007 - 04:38 PM
Post #1

New D.I.C Head
*

Joined: 2 Nov, 2007
Posts: 14


My Contributions
Hey, I need to be able to read integers from a .txt file and output them sorted in a new .txt file. We are given code to do this, but I don't really understand it or know how to add it to my program. If someone could help me I would greatly appreciate it. Here is the code I was given:

CODE

/*
Sample code for reading input from a file
Originally written by John O'Gorman IV
Modified to remove specific function calls, etc.

CS122
October 23, 2007
*/

#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>

//MAIN
int main(int argc, const char *argv[])
{
    //Character array for storing strings that are read in from the file
    char string[200];

    //Create a fileObject and read from it
    FILE *fileObject;
    fileObject = fopen(argv[1], "r");

    //strcmp(str, "Insert")
    //For string comparison
    //
    //strcmp returns 0 == equal
    //1 = right is larger
    //-1 = left is larger

    while(!feof(fileObject))
    {
        fflush(stdout);
        int assocvalue;
        
        //Get the next string in the file
        fscanf(fileObject, "%s", string);
        
    //EOF ENCOUNTERED    
        if(feof(fileObject))
        {
            break;
        }
        else if(strcmp(string,"Insert") == 0)
        {
    //INSERT
            fscanf(fileObject, "%d", &assocvalue);
            // Call insert here
        }
        else if(strcmp(string,"Find") == 0)
        {
    //FIND
            fscanf(fileObject, "%d", &assocvalue);
            // Call find here
        }
        else if(strcmp(string,"Delete") == 0)
        {
    //DELETE
            fscanf(fileObject, "%d", &assocvalue);
            // Call delete here
        }
        else if(strcmp(string,"FindMin") == 0)
        {
    //MIN
            // Call findMin here
        }
        else if(strcmp(string,"FindMax") == 0)
        {
    //MAX
            // Call findMax here
        }
        else if(strcmp(string,"GetDepth") == 0)
        {
    //GETHEIGHT
            // Call GetHeight here
        }
        else if(strcmp(string,"PrintPreOrder") == 0)
        {
    //PRINT PREORDER
            // Print preorder here
        }
        else if(strcmp(string,"PrintInOrder") == 0)
        {
    //PRINT INORDER
            // Print inorder here
        }
        else if(strcmp(string,"PrintPostOrder") == 0)
        {
    //PRINT POSTORDER
            // Print postorder here
        }
    //COMMAND WAS NOT RECOGNIZED
        else
        {
            printf("COMMAND NOT RECOGNIZED!\n");
        }
    }
    
    fclose(fileObject);

    //END!
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Reading And Outputting .txt Files
1 Dec, 2007 - 07:40 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well essentially you were given a skeleton program. At the top you open the file using fopen() which expects you to pass a filename to the program using the command prompt...

c:\blahblah.exe c:\myinputfile.txt

From there it loops through the file reading each line (using fscanf) until it reaches the end of the file (file EOF aka END OF FILE). As it reads each line, it evaluates it for special command words like insert, find or delete. If it encounters these words, it then reads in the value following the word and this is where you are then to write functions which carry out the function.

For instance, you read in the file and encounter the command "find" which then you read in the number following the command. So the line in the file would look like... FIND 12

This program already reads the 12 in for you, but then you are expected to write a function called "find()" and pass that 12 to your function which would then search an array or something else and return something like the position it was found in (it doesn't cover that part unfortunately).

So where you see it say "call find here" you might do this...

CODE

else if(strcmp(string,"Find") == 0)
{
     //FIND
     fscanf(fileObject, "%d", &assocvalue);

     // Call find here
     int position = myCustomFindFunction(assocvalue);
     if (position > 0) {
          cout << "Found the number " << assocvalue << " at position: " << position << endl;
     }
}


You are to write these custom functions for each of the commands in the else if statement that says "write such and such here".

Hope that makes sense to you. Good luck!

"At DIC we be code deciphering if else statement loving code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/9/09 05:49AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month