3 Replies - 2033 Views - Last Post: 20 March 2011 - 03:57 AM Rate Topic: -----

#1 xenofied   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-July 06

A simple text parse. PARSE.C

Posted 30 July 2006 - 04:45 AM

Description: A simple program to read in a sentence of words within a file and parse them into seperate lines of words.
/****************************************************************/
/*Program: PARSE.C                                              */
/*Author:  Alexander A.K.A Xenofied                                */
/*Platform: Windows Operating System(tested on XP PIII, PIV, PM)*/
/*Compiler: Free Dev-C++ Compiler (@ http://www.bloodshed.com/) */
/*Comment: Program to read in a sentence of words within a file */
/*         and parse them into seperate lines of words.         */
/****************************************************************/

#include <stdio.h>   /*All c program must have this*/
#define bufsize 1024 /*A defined integer for our buffer size*/

int main(){          /*Program entry point*/
     FILE* fp;       /*Declare file pointer variable*/
     char *buf[bufsize], *tok, fname[15];
     
     printf("Enter filename: ");
     scanf("%s",&fname);
     
     /*If file doesn't exist or filetype isn't allowed exit and*/
     /*error message & return (1) control to the OS*/
     if ((fp = fopen(fname,"rt")) == NULL){ 
             fprintf(stderr,"Error opening file: %sn",fname);
             return 1;
      }
      
     printf("n"); /*Newline effect*/
     
     /*Read into the buffer contents within thr file stream*/
     while(fgets(buf, bufsize, fp) != NULL){
                      
          /*Here we tokenize our string and scan for " n" characters*/
          for(tok = strtok(buf," n");tok;tok=strtok(0," n")){
                  printf("%sn",tok);
          }                  
          fclose(fp); /*Close file*/
     }/*Continue until EOF is encoutered*/
     printf("n");
     system("pause"); /*System delay function (windows)*/
     return 0; /*Executed without errors*/
}/*End main*/

/***********************PARSE.TXT (CONTENT)**********************/
/* "Move each word to the next line"                            */
/***********************PARSE.EXE (OUTPUT)***********************/
/*  C:>parse                                                   */
/*  Enter filename: parse.txt                                   */
/*                                                              */
/*  Move                                                        */
/*  each                                                        */
/*  word                                                        */
/*  to                                                          */
/*  the                                                         */
/*  next                                                        */
/*  line.                                                       */
/*                                                              */
/*  Press any key to continue . . .                             */
/****************************************************************/



Is This A Good Question/Topic? 0
  • +

Replies To: A simple text parse. PARSE.C

#2 hallizh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-March 08

Re: A simple text parse. PARSE.C

Posted 27 January 2009 - 02:28 AM

Thanks, the following lines are not too clear for me though, comments on them would hurt :) char *buf[bufsize], *tok, fname[15]; for(tok = strtok(buf," \n");tok;tok=strtok(0," \n")){ Helped me alot though :)
Was This Post Helpful? 0
  • +
  • -

#3 hallizh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 09-March 08

Re: A simple text parse. PARSE.C

Posted 27 January 2009 - 02:29 AM

Right, unreadable, sorry.
Was This Post Helpful? 0
  • +
  • -

#4 midiangr   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 20-March 11

Re: A simple text parse. PARSE.C

Posted 20 March 2011 - 03:57 AM

great, very nice paradigm. however, i think that line 36 "fclose(fp); /*Close file*/" should be after the "for", in order to read more than one lines. Thnx!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1