[xin]hàm đọc file .txt từ SD Card sử dụng thư viện FatFs

Collapse
X
 
  • Giờ
  • Show
Clear All
new posts
  • hoanghuybao
    Thành viên mới
    • Oct 2010
    • 3

    #1

    [xin]hàm đọc file .txt từ SD Card sử dụng thư viện FatFs

    Chào mọi người !
    Ai có hàm đọc file .txt từ SD Card sử dụng thư viện FatFs trên chíp STM32F103C8 hay làm qua rồi cho mình xin code.
    Code mình viết bị lỗi chưa sửa được.
    Video:lỗi đọc 2 file .txt kế tiếp nhau - YouTube
    thanks
  • chumanh
    Thành viên tích cực
    • Nov 2009
    • 256

    #2
    hàm f_read là sử dụng để đọc file trong thư viện chan FatFs đó bạn.

    Comment

    • hoanghuybao
      Thành viên mới
      • Oct 2010
      • 3

      #3
      //Read file txt
      char* ReadFileTxt(char *name)
      {
      char *result;
      int totalBytesRead = 0;

      for(i=0;i<strlen(buffer);i++)
      buffer[i]=NULL;
      printf("\n name = %s",name);

      f_mount(0,&fsa);
      res = f_open(&fsrc,name, FA_READ);
      for ( {
      res = f_read(&fsrc, buffer, sizeof(buffer), &br); /* Read a chunk of file */
      if (res || !br) break; /* Error or end of file */
      //totalReadTimes++;
      totalBytesRead+=br;
      }

      /* IMPORTANT */
      result= (char*)malloc((totalBytesRead+1) * sizeof(char));
      strncpy(result,buffer,totalBytesRead);
      printf("\n body = %s",result);
      f_close(&fsrc);
      printf("\n Bytes Read = %d",totalBytesRead);
      return result;
      }

      hàm mình tự viết,nhưng bị lỗi

      Comment

      • nguyenvanvui
        Thành viên tích cực
        • May 2009
        • 168

        #4
        bạn nên dùng thư viện FS_CM3.lib thì hay hơn.
        code để đọc file .txt:

        static void file_read (char *par) {
        char *fname,*next;
        FILE *f;
        int ch;

        fname = get_entry (par, &next);
        if (fname == NULL) {
        printf ("\nFilename missing.\n");
        return;
        }
        printf("\nRead data from file %s\n",fname);
        f = fopen (fname,"r"); /* open the file for reading */
        if (f == NULL) {
        printf ("\nFile not found!\n");
        return;
        }

        while ((ch = fgetc (f)) != EOF) { /* read the characters from the file */
        putchar (ch); /* and write them on the screen */
        }
        fclose (f); /* close the input file when done */
        printf ("\nFile closed.\n");
        }
        các vấn đề về sdcard, usb, tcp/ip, upgrate firmware,...
        trên các dòng chip: stm32, lpc of nxp
        please cell phone: 01649895559

        Comment

        Về tác giả

        Collapse

        hoanghuybao Tìm hiểu thêm về hoanghuybao

        Bài viết mới nhất

        Collapse

        Đang tải...