Reading output into a text file

This is a discussion on Reading output into a text file within the C Programming forums, part of the General Programming Boards category; Hi, please reference this with my code below. I have created 3 text files (name.txt, name2.txt and name3.txt) Each of ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    Reading output into a text file

    Hi, please reference this with my code below.

    I have created 3 text files (name.txt, name2.txt and name3.txt) Each of the 3 text files contains 3 words, so 9 words are displayed in total at the end of my program (this is why argc !=4). The only thing i need to do now is get it to take all the 9 words and write them to an output file, one word per line.

    Thanks very much for any assistance anyone is able to give me with this problem.


    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
      int i;
      if ( argc != 4 ) /* argc should be 2 for correct execution */
        {
          /* We print argv[0] assuming it is the program name */
          printf( "usage: %s filename", argv[0] );
        }
        else
          {
            // We assume argv[1] is a filename to open
            for(i = 1; i < 4; i = i+1) // for loop (starting; while_true; do_this).
              {
                FILE *file = fopen( argv[i], "r" );
    
                /* fopen returns 0, the NULL pointer, on failure */
                if ( file == 0 )
                  {
                    printf( "Could not open file\n" );
                  }
            else
              {
                int x;
                /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
                while  ( ( x = fgetc( file ) ) != EOF )
                  {
                    printf( "%c", x );
                  }
              }
                printf(" \n"); //ensures first word from next text file is on a new line. 
                fclose( file );
              }
    
          }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Use fopen to open a file and fprintf to print to it and fclose to close it when you're done.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    Sorry i wasn't very clear.

    It needs to create a new, foruth text file containing all the words. Not add to the current ones.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    This code reads (based off your code) every char or byte at a time and writes every byte to a sperate file. This isn't what you are saying you want to do. Technically it is wrong.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( int argc, char *argv[] )
    {
        int files = argc-1, i = 0, x = 0;
        FILE *infile, *outfile;
    
            if ( (outfile = fopen("Textfile.txt", "w")) == NULL )
            {
                    fprintf(stderr, "Can't Open Textfile..txt for out put\n");
                    exit(2);
            }
    
    
        for(i = 1; i <= files; i++)
        {
                    printf("opening FILE filename = &#37;s\n",argv[i]);
    
                    if ( (infile = fopen( argv[i], "r")) == NULL )
                    {
                            fprintf(stderr, "Can't Open file %s\n", argv[i]);
                            continue;
                    }
                    else
                    {
                            while ( (x = fgetc(infile)) != EOF )
                            {
                                    putchar(x);
                                    fputc ( (int) x , outfile );
                            }
                    }
                    fclose( infile );
        }
    return 0;
    }
    What you really want to be doing is something more like this (I think). This code reads every word and outputs the entire word to a seperate combined file. Which one are you really needing to do?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( int argc, char *argv[] )
    {
            int files = argc-1, i = 0, x = 0;
            char word[80];
            FILE *infile, *outfile;
    
            if ( (outfile = fopen("WordList.txt", "w")) == NULL )
            {
                    fprintf(stderr, "Can't Open Textfile..txt for out put\n");
                    exit(2);
            }
    
    
            for(i = 1; i <= files; i++)
            {
                    printf("opening FILE filename = %s\n",argv[i]);
    
                    if ( (infile = fopen( argv[i], "r")) == NULL )
                    {
                            fprintf(stderr, "Can't Open file %s\n", argv[i]);
                            continue;
                    }
                    else
                    {
                            while ( !feof(infile ) )
                            {
                                    fscanf(infile, "%s", word);
                                    fprintf(outfile, "%s", word);
                            }
                    }
                    fclose( infile );
            }
    return 0;
    }
    Last edited by sillyman; 04-23-2008 at 12:16 PM.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    Awsome, i understand it better now. It was the second portion of code i needed. Thanks very much. Two questions though, first in Wordt.txt it writes them all one line with no spaces. How do i get it to write them with each word on a new line?

    Also, i need to get it so that it checks if the word it is outputting has already been done (IE, if there are two instances of the same word) so that it is 'discarded' and not wirtten to WordList.txt twice. Are you able to also give me some assistance with this?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    1) You are looking for the end of line character (\n) it tells the command line to drop down one row.
    2) You have to create a data store like an character array or a linked list. My example uses a linked list as I don't know how many words will be in a real world file. A character array could over flow and crash the program. With a linked list, you can go until you fill up memory (RAM).
    If your files start getting huge, you will need to read the main data file "WordList.txt" for every word in every file starting from the beginning each time.

    Is this a homework assignment?

    Code:
         1  #include <stdio.h>
         2  #include <stdlib.h>
         3  #include <string.h>
         4  
         5  struct wordlist {
         6          struct wordlist *next;
         7          char single_word[80];
         8  };
         9  
        10  
        11  
        12  int main ( int argc, char *argv[] )
        13  {
        14          int files = argc-1, i = 0, x = 0, flag=0;
        15          char word[80];
        16          FILE *infile, *outfile;
        17          struct wordlist *start_list;
        18          struct wordlist *temp_list;
        19          struct wordlist *data;
        20          struct wordlist *last_list;
        21  
        22          start_list = last_list = NULL;
        23  
        24          data = (struct wordlist *)malloc(sizeof(struct wordlist));
        25          if(!data) 
        26          {
        27                  fprintf(stderr, "Out of memory\n");
        28                  exit(2); 
        29          }
        30          else 
        31          {
        32                  data->next = NULL;
        33                  start_list = data;
        34                  last_list = data;
        35          }
        36  
        37          if ( (outfile = fopen("WordList.txt", "w")) == NULL )
        38          {
        39                  fprintf(stderr, "Can't Open Textfile..txt for out put\n");
        40                  exit(2);
        41          }
        42  
        43          for(i = 1; i <= files; i++)
        44          {
        45                  if ( (infile = fopen( argv[i], "r")) == NULL )
        46                  {
        47                          fprintf(stderr, "Can't Open file &#37;s\n", argv[i]);
        48                          continue;
        49                  }
        50                  else
        51                  {
        52                          fscanf(infile, "%s", word)  ;
        53                          while ( !feof(infile) )
        54                          {       
        55                                  temp_list = start_list;
        56                                  flag=0;
        57                                  while ( temp_list )
        58                                  {
        59                                          if ( strcmp(temp_list->single_word, word) == 0 )
        60                                          {
        61                                                  flag=1; 
        62                                                  break;
        63                                          }
        64                                          temp_list = temp_list->next;
        65                                  }
        66  
        67                                  if ( flag == 0 )
        68                                  {
        69                                          data = (struct wordlist *)malloc(sizeof(struct wordlist));
        70                                          if(!data) 
        71                                          {
        72                                                  fprintf(stderr, "Out of memory\n");
        73                                          }
        74                                  
        75  
        76                                          last_list->next = data;
        77                                          data->next = NULL;
        78                                          last_list = data;
        79  
        80                                          strcpy(data->single_word, word);
        81                                          printf("%s\n", word);
        82                                          fprintf(outfile, "%s\n", word);
        83                                  }
        84  
        85  
        86                                  fscanf(infile, "%s", word)  ;
        87                          }  
        88                  }
        89                  fclose( infile );
        90          }
        91  return 0; 
        92  }
    Last edited by sillyman; 04-23-2008 at 02:42 PM.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    Thank you for your response. As for the homework question, the answer is "not as such". I'm not enrolled on a C programming course and am just trying to learn to increase my skills set. However, i have purchased several books used by educational estaliblishments and this program was a suggested excercise in one of those books.

    Your new code is very complicated (for me anyway!) and conatins lots of commands i have not come across before. In terms of keeping this simple let's stick with the second bit of code you posted orginally. I completely forgot about the "/n" command, thank you they are now displayed on seperate lines. Let's forget about checking for second instances. I thought maybe it would be possible to query the output file once it was created and remove any duplicates that way? But thinking about it they arn't stored in any variables so that's not going to work is it?

    Another part of the excersie is to sort the words alphabetically. I've used the bubble sort feature in Pascal before but have never done any sorting in C. Are you able to start me off with some sorting code?

    Once again, i appreciate all the help you are giving me. This is a huge leaning curve for me.

  8. #8
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    I would use the qsort .... bubble sorts are fun and good for smaller datasets.

    Anyway here is the array example followed by your request for the output file example too.

    9 element array
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( int argc, char *argv[] )
    {
            int files = argc-1, i = 0, x = 0, flag=0, a=0;
            char word[80], list[9][80];
            FILE *infile, *outfile;
    
            if ( (outfile = fopen("WordList.txt", "w")) == NULL )
            {
                    fprintf(stderr, "Can't Open Textfile..txt for out put\n");
                    exit(2);
            }
    
            for(i = 1; i <= files; i++)
            {
                    if ( (infile = fopen( argv[i], "r")) == NULL )
                    {
                            fprintf(stderr, "Can't Open file &#37;s\n", argv[i]);
                            continue;
                    }
                    else
                    {
                            fscanf(infile, "%s", word)  ;
                            while ( !feof(infile) )
                            {       
                                    a=flag=0;
                                    while ( list[a++][0] )
                                    {
                                            if ( strcmp(list[a], word) == 0 )
                                            {
                                                    flag=1; 
                                                    break;
                                            }
                                    }
                                    if ( flag == 0 )
                                    {
                                            strcpy(list[a], word);
                                            printf("%s\n", word);
                                            fprintf(outfile, "%s\n", word);
                                    }
                                    fscanf(infile, "%s", word)  ;
                            }  
                    }
                    fclose( infile );
            }
    return 0; 
    }
    File storage example
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( int argc, char *argv[] )
    {
            int files = argc-1, i = 0, x = 0, flag=0;
            char word[80], list[80];
            FILE *infile, *outfile, *datafile;
    
            if ( (outfile = fopen("WordList.txt", "w")) == NULL )
            {
                    fprintf(stderr, "Can't Open Textfile..txt for out put\n");
                    exit(2);
            }
    
            if ( (datafile = fopen("WordList.txt", "r")) == NULL )
            {
                    fprintf(stderr, "Can't Open Testfile for read\n");
                    exit(2);
            }
    
            for(i = 1; i <= files; i++)
            {
                    if ( (infile = fopen( argv[i], "r")) == NULL )
                    {
                            fprintf(stderr, "Can't Open file %s\n", argv[i]);
                            continue;
                    }
                    else
                    {
                            fscanf(infile, "%s", word)  ;
                            while ( !feof(infile) )
                            {       
                                    rewind(datafile);
                                    flag=0;
                                    fscanf(datafile, "%s", list)  ;
                                    while ( !feof(datafile) )
                                    {
                                            fscanf(datafile, "%s", list)  ;
                                            if ( strcmp(list, word) == 0 )
                                            {
                                                    flag=1; 
                                                    break;
                                            }
                                    }
                                    if ( flag == 0 )
                                    {
                                            printf("%s\n", word);
                                            fprintf(outfile, "%s\n", word);
                                            fflush(outfile);
                                    }
                                    fscanf(infile, "%s", word)  ;
                            }  
                    }
                    fclose( infile );
            }
    return 0; 
    }
    hope it helps to see the different ways to handle your problem. btw, I am learning to code this stuff myself too. I don't do this stuff for real either. I just didn't want to be doing some high school kids work.
    Last edited by sillyman; 04-24-2008 at 06:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  2. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Reading a text file
    By AmazingRando in forum C Programming
    Replies: 2
    Last Post: 09-08-2003, 11:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21