locked
Opening a .dat file in C

    Question

  • Hi everyone,

    I am trying to open a text file with visual studio and I cannot seem to get it to work.

    My code right now is:

    #include <stdio.h>
    #include <stdlib.h>
    #define filename "test1.dat"

    int main ()

    {
    // Local Declarations
        FILE* spNumbers;
        int num;

    // Statements
        spNumbers = fopen("test1.dat", "r");
        if(spNumbers == NULL)
             {
                   printf("\aCannot open file\n");
                   exit(100);
        }
        fclose(spNumbers);
        return 0;
    } // main

    Any insight would be greatly appreciated. Thanks in advance.

    Tuesday, July 26, 2011 5:37 AM

Answers

  • what do you mean when you say that it don't work? Du you enter your if-sentence and print "cannot open file", do you get an error or don't the program do what you expected it to do?

    If you get an error, you must post the error. If you go in to the if sentence, you probably don't have test1.dat in the correct directory. If you were expecting the program to actually open the file (in a new window or something) that is not how fopen works. 

     

    edit:

    If It was the last problem, try something like this.

    #include <stdio.h>
    
    int main()
    {
     FILE * spNumbers;
     char buffer [100];
    
     spNumbers= fopen("test1.dat", "r");
     if (spNumbers== NULL) 
      perror("Error opening file");
     else{
      while( !feof (spNumbers) ){
      fgets(buffer , 100 , spNumbers);
      fputs(buffer , stdout);
      }
      fclose(spNumbers);
     }
     return 0;
    }


    • Proposed as answer by Skrova Tuesday, July 26, 2011 12:46 PM
    • Marked as answer by shantel10 Tuesday, July 26, 2011 1:39 PM
    Tuesday, July 26, 2011 8:16 AM

All replies

  • what do you mean when you say that it don't work? Du you enter your if-sentence and print "cannot open file", do you get an error or don't the program do what you expected it to do?

    If you get an error, you must post the error. If you go in to the if sentence, you probably don't have test1.dat in the correct directory. If you were expecting the program to actually open the file (in a new window or something) that is not how fopen works. 

     

    edit:

    If It was the last problem, try something like this.

    #include <stdio.h>
    
    int main()
    {
     FILE * spNumbers;
     char buffer [100];
    
     spNumbers= fopen("test1.dat", "r");
     if (spNumbers== NULL) 
      perror("Error opening file");
     else{
      while( !feof (spNumbers) ){
      fgets(buffer , 100 , spNumbers);
      fputs(buffer , stdout);
      }
      fclose(spNumbers);
     }
     return 0;
    }


    • Proposed as answer by Skrova Tuesday, July 26, 2011 12:46 PM
    • Marked as answer by shantel10 Tuesday, July 26, 2011 1:39 PM
    Tuesday, July 26, 2011 8:16 AM
  • It doesn't display an error message; when I try running it, it just shows up blank... I was expecting it to read the numbers.

    Your program, did, however work. Is there any way to do the same thing without using arrays?

     

    Thank you so much for your help!

    Tuesday, July 26, 2011 1:18 PM
  • an example from msdn "_read"


    // crt_read.c /* This program opens a file named crt_read.txt * and tries to read 60,000 bytes from * that file using _read. It then displays the * actual number of bytes read. */ #include <fcntl.h> /* Needed only for _O_RDWR definition */ #include <io.h> #include <stdlib.h> #include <stdio.h> #include <share.h> char buffer[60000]; int main( void ) { int fh; unsigned int nbytes = 60000, bytesread; /* Open file for input: */ if( _sopen_s( &fh, "crt_read.txt", _O_RDONLY, _SH_DENYNO, 0 ) ) { perror( "open failed on input file" ); exit( 1 ); } /* Read in input: */ if( ( bytesread = _read( fh, buffer, nbytes ) ) <= 0 ) perror( "Problem reading file" ); else printf( "Read %u bytes from file\n", bytesread ); _close( fh ); }


    Tuesday, July 26, 2011 2:35 PM