Cant scan file in C

2019-09-25 03:56发布

I am cant seem to get my simple script to read the file. All i am trying to do is read letters into an array. I just get random characters and it is driving me crazy. I have the file in the Debug folder, what else can go wrong? I am 100 percent sure everything is correct.

Here is my code:

FILE * ifp;
   ifp = fopen("letters.txt", "r");


   int i;
   int bound = 20;
   char data[20];


    for(i =0; i<bound; i++){
        fscanf(ifp, "%s", &data[1]);
        if (ifp == NULL){
            return;
        }
        printf("Data %d = %c\n", i, data[i]);

    }

标签: c file scanf
3条回答
甜甜的少女心
2楼-- · 2019-09-25 04:14

Here's an example:

#include <stdio.h>

#define MAX_LINES 10
#define MAX_LINE  80
#define FILE_NAME "lines.txt"

int
main (int argc, char *argv[])
{
  char lines[MAX_LINES][MAX_LINE];
  FILE *fp;
  int i = 0;

  if (!(fp = fopen(FILE_NAME, "r"))) {
    perror ("file open error");
    return 1;
  }

  while (i < MAX_LINES) {
    if (fgets (lines[i], MAX_LINE, fp)) {
      printf ("Line[%d]=%s", i, lines[i]);
      i++;
    } else {
      printf ("End of file: closing %s\n", FILE_NAME);
      break;
    }
  }
  fclose (fp);
  return 0;
}

Sample output:

$ gcc -o x1 -Wall -pedantic -g x1.c
$ ./x1
Line[0]=Come and listen to my story
Line[1]='Bout a man named Jed
Line[2]=Poor mountaineer,barely kept his fam'ly fed
End of file: closing lines.txt

Note:

  1. We're reading a line at a time

  2. We're reading each line into an array

  3. We define the #/elements in the array (MAX_LINES) and the maximum size of each string (MAX_LINE) ahead of time

  4. We check for "file open error" (and return "non-zero" accordingly)

  5. We use "fgets()" to safely read a line (without risking a buffer overrun).

If you wanted to read a character at a time, the program could be as simple as:

 char string[MAX_LINE], *c;
 ...
 if (!(fp = fopen(FILE_NAME, "r"))) {
    perror ("file open error");
    return 1;
 }
 ...     
 c = string;
 while ((*c = fgetc (fp)) != EOF)
    c++;

One problem with the second example is that it doesn't check for string length >> MAX_LINE. But hopefully it helps give you some ideas...

查看更多
手持菜刀,她持情操
3楼-- · 2019-09-25 04:15

Hmm could it be because

 &data[1]

should be just

data

or the equivalent

&data[0]

which is likely what you intended.

?

Only other reason I can think of is, the line you are reading is greater than 20 characters and its overflowing the data array.

Have a look at this example: http://www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm

查看更多
在下西门庆
4楼-- · 2019-09-25 04:29

The following code

1) checks for errors
2) compiles cleanly
3) works on a test.txt file I produced.

#include <stdio.h>
#include <stdlib.h>

#define bound (20)

int main( void )
{
    FILE * ifp;

    if( NULL == (ifp = fopen("letters.txt", "r") ) )
    { // then, fopen failed
        perror( "fopen for letters.txt failed");
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful


    int i;

    char data[bound];


    for(i =0; i<bound; i++)
    {
        if( 1 != fscanf(ifp, "%c", &data[i]) )
        { // then fscanf failed
            perror( "fscanf failed" );
            exit( EXIT_FAILURE );
        }

        // implied else, fscanf successful

        printf("Data %d = %c\n", i, data[i]);
    }
    return(0);
} // end function: main
查看更多
登录 后发表回答