Problem reading file

This is a discussion on Problem reading file within the C Programming forums, part of the General Programming Boards category; The following is my code to read a file. Code: #include <stdio.h> #include <ctype.h> #include <string.h> main(int argc, char *argv[]) ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Problem reading file

    The following is my code to read a file.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    main(int argc,  char *argv[])
    {   
       FILE *cfPtr;
       char xyz[10];
    	if( argc != 2 )
    	{    printf("Argument provided incorrect\n");
    	}else{
    	   if((cfPtr = fopen(argv[1],"r")) == NULL){
    		printf("file couldnt not be open");
    	   }else{
    		fscanf(cfPtr, "%s", xyz);
    
    		while(!feof(cfPtr)){
    			printf("%s\n", xyz);
    			fscanf(cfPtr, "%s", xyz);
    		}
    		 fclose(cfPtr);
    		 }
    	}
    }

    The following is the file that i wanted to read.

    Code:
    #include <stdio.h>
    
    main(int argc,  char *argv[])
    { int c, i;
    
      if (argc == 1) /* No command line arguments */
        while ((c = getchar()) != EOF) /* echo whatever is typed */
          putchar(c);
    
      else /* at least one command line argument */
      { printf("\n");
        for(i = 0; i < argc; i++)
          printf("argv[%d]=\"%s\"\n", i, argv[i]); /* print each argument */
        printf("\n");                              /* NB argv[0] = program name */
      }
    }
    My program manage to read the following content only. It comes out error msg after that. I tried to debug and it state
    "00405410 mov dword ptr [edx+4],ecx". How can i solved this problem so that it is able to read till the end of the file ??

    Code:
    #include <stdio.h>
    
    main(int argc,  char *argv[])
    { int c, i;
    
      if (argc == 1) /* No command line arguments */
        while ((c = getchar()) != EOF) /* echo whatever is typed */
          putchar(c);
    
      else /* at least one command line argument */
      { printf("\n");

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    41
    I manage to figure out dy. The problem is due to small memory allocation.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 12:49 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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