Thread: reading a file

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    37

    reading a file

    my text.txt file
    Code:
    LDC 1,2 #444
    LDC 1,2 #444
    my program:
    Code:
    #include <stdio.h>
    #include <string.h>
    char file_array[10000];
    
    
    void
    readfile(char *file)
    {
      char ch;
      int i = 0;
      FILE *pfile = fopen(file, "r");
    
      if (pfile == NULL)
         {
             puts("New Files");
         }
      else
         {
             while((ch = fgetc(pfile)) != EOF)
               {
                   file_array[i] = ch;
                   i++;
               }
             file_array[i] = '\0';
             fclose (pfile);
        }
    }
    
    int main(int argc, char **argv)
         
    {
      char *filename = argv[1];
      //char test_string[50]="PRINT 1,2";
      char *sub_string;
    
      readfile(filename);
      printf("%s\n", strtok(file_array, " "));
      while ( (sub_string=strtok(NULL, ",")) != NULL)
      {
        printf("%s\n", sub_string);
      }
      return 0;
    }
    at the moment my output is

    a.out text.txt
    LDC
    1
    2 #444
    LDC 1
    2 #444

    i want to modify it so it prints out this
    LDC
    1
    2
    LDC
    1
    2

    ignoring everything after and including the #

    how do i do this?
    Last edited by 8ball; 06-06-2004 at 10:57 PM.

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Give this a shot.

    Code:
    void
    readfile(char *file)
    {
      char ch;
      int i = 0;
      int comment = 0;
      FILE *pfile = fopen(file, "r");
    
      if (pfile == NULL)
    	 {
    		 puts("New Files");
    	 }
      else
    	 {
    		 while((ch = fgetc(pfile)) != EOF)
    		   {
    			   if (ch == '#')
    			   {
    				 comment = 1;
    			   }
    			   else if (ch == '\n')
    			   {
    				 comment = 0;
    			   }
     
    			   if (comment == 0)
    			   {
    				  file_array[i] = ch;
    				  i++;
    			   }
    		   }
    		 file_array[i] = '\0';
    		 fclose (pfile);
    	}
    }

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    37
    when my input is
    LDC 1,2 #444
    LDC 1,2 # dkljdlkdj
    Print -4 #dldkdl;

    its giving me this output
    LDC
    1
    2
    LDC 1
    2
    Print -4

    but i want it to be like this
    LDC
    1
    2
    LDC
    1
    2
    Print
    -4

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well first you need to read the file a line at a time, rather than reading the whole file into an array

    Here are the first couple of steps
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, pfile ) != NULL ) { /* read each line */
        char *hash = strchr( buff, '#' );  /* is there a comment? */
        if ( hash != NULL ) *hash = '\0'; /* line is now commentless */
        /* now do your strtok'ing */
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    37
    will i need linked lists?

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by 8ball
    my text.txt file
    Code:
    LDC 1,2 #444
    LDC 1,2 #444
    my program:
    Code:
      char ch;
    
             while((ch = fgetc(pfile)) != EOF)
    Your code original code worked?
    The one who says it cannot be done should never interrupt the one who is doing it.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    37
    nope...

  8. #8
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by 8ball
    nope...
    fgetc() returns an int, EOF is usually an int value that cannot be represented as a char, so you might end up with an infinite loop unless you declare ch as a int.
    The one who says it cannot be done should never interrupt the one who is doing it.

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    37
    Quote Originally Posted by Salem
    Well first you need to read the file a line at a time, rather than reading the whole file into an array

    Here are the first couple of steps
    Code:
    char buff[BUFSIZ];
    while ( fgets( buff, BUFSIZ, pfile ) != NULL ) { /* read each line */
        char *hash = strchr( buff, '#' );  /* is there a comment? */
        if ( hash != NULL ) *hash = '\0'; /* line is now commentless */
        /* now do your strtok'ing */
    }
    where do i place this?

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    37
    this is what my code looks like now
    Code:
    #include <stdio.h>
    #include <string.h>
    char file_array[10000];
    
    
    /*void
    readfile(char *file)
    {
      char ch;
      int i = 0;
      FILE *pfile = fopen(file, "r");
    
      if (pfile == NULL)
         {
             puts("New Files");
         }
      else
         {
             while((ch = fgetc(pfile)) != EOF)
               {
    	   	if (ch==35)
    			break;
    			else
    			{
                   file_array[i] = ch;
                   i++;
    	       }
               }
             file_array[i] = '\0';
             fclose (pfile);
        }
    }
    */
    void
    readfile(char *file)
    {
      int ch;
      int i = 0;
      int comment = 0;
      FILE *pfile = fopen(file, "r");
    
      if (pfile == NULL)
    	 {
    		 puts("New Files");
    	 }
      else
    	 {
    		 while((ch = fgetc(pfile)) != EOF)
    		   {
    			   if (ch == '#')
    			   {
    				 comment = 1;
    			   }
    			   else if (ch == '\n')
    			   {
    				 comment = 0;
    			   }
     
    			   if (comment == 0)
    			   {
    				  file_array[i] = ch;
    				  i++;
    			   }
    		   }
    		 file_array[i] = '\0';
    		 fclose (pfile);
    	}
    }
    
    void breakup(char *file_array)
    {
      
    
      	//char test_string[50]="PRINT 1,2";
      	char *sub_string;
    	printf("%s\n", strtok(file_array, " "));
      	while ( (sub_string=strtok(NULL, ",")) != NULL)
      	{
        		printf("%s\n", sub_string);
      	}
    }
    
    int main(int argc, char **argv)
    {
    char *filename = argv[1];
      	readfile(filename);
      	breakup(file_array);
      	return 0;
    }
    and my output now is
    LDC
    1
    2
    LDC 1
    2
    Print -4

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > where do i place this?
    OK, the whole thing
    Code:
    int main ( int argc, char **argv ) {
        char buff[BUFSIZ];
        FILE *pfile = fopen(argv[1], "r");
        while ( fgets( buff, BUFSIZ, pfile ) != NULL ) { /* read each line */
            char *sub_string;
            char *hash = strchr( buff, '#' );  /* is there a comment? */
            if ( hash != NULL ) *hash = '\0'; /* line is now commentless */
            /* now do your strtok'ing */
            printf("%s\n", strtok(buff, " "));
            while ( (sub_string=strtok(NULL, ",\n")) != NULL)
            {
                    printf("%s\n", sub_string);
            }
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    37
    thanks alot for that

    what would be the parameter if i want to place all that in a function?

  13. #13
    Registered User
    Join Date
    May 2004
    Posts
    37
    Code:
    void breakup()
    {
    char buff[BUFSIZ];
        
        while ( fgets( buff, BUFSIZ, pfile ) != NULL ) { /* read each line */
            char *sub_string;
            char *hash = strchr( buff, '#' );  /* is there a comment? */
            if ( hash != NULL ) *hash = '\0'; /* line is now commentless */
            /* now do your strtok'ing */
            printf("%s\n", strtok(buff, " "));
            while ( (sub_string=strtok(NULL, ",\n")) != NULL)
            {
                    printf("%s\n", sub_string);
            }
        }
    }
    
    int main ( int argc, char **argv ) {
      FILE *fp = fopen(argv[1], "r");
      breakup(fp);
    
      return 0;
    }
    is this the way to do it?

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > is this the way to do it?
    If you fix breakup() to accept a parameter, yes
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    37
    void breakup(char *pfile)
    ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM