Read a CSV File from a Flash drive (Using C++, Ubuntu 12.04)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robotarduino
    New Member
    • Mar 2013
    • 1

    Read a CSV File from a Flash drive (Using C++, Ubuntu 12.04)

    I want to read a file from my flash drive called text.csv. However, I cannot even open the port where my flash drive is connected. This is the code that I am using, but I get error since the first part. When I run the program it says "fopen Error". I am using Ubuntu 12.04. Please help.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    #define SIZE 1
    #define NUMELEM 5
    
    int main(void)
    {
        FILE* fd = NULL;
        char buff[100];
        memset(buff,0,sizeof(buff));
    
        fd = fopen("/dev/sdc/test.csv","r");
    
        if(NULL == fd)
        {
            printf("\n fopen() Error!!!\n");
            return 1;
        }
    
        printf("\n File opened successfully through fopen()\n");
    
        if(SIZE*NUMELEM != fread(buff,SIZE,NUMELEM,fd))
        {
            printf("\n fread() failed\n");
            return 1;
        }
    
        printf("\n Some bytes successfully read through fread()\n");
    
        printf("\n The bytes read are [%s]\n",buff);
    
        if(0 != fseek(fd,11,SEEK_CUR))
        {
            printf("\n fseek() failed\n");
            return 1;
        }
    
        printf("\n fseek() successful\n");
    
        if(SIZE*NUMELEM != fwrite(buff,SIZE,strlen(buff),fd))
        {
            printf("\n fwrite() failed\n");
            return 1;
        }
    
        printf("\n fwrite() successful, data written to text file\n");
    
        fclose(fd);
    
        printf("\n File stream closed through fclose()\n");
    
        return 0;
    }
    Last edited by Banfa; Mar 26 '13, 01:58 PM. Reason: Added code tags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Plug in your USB device and use ls in a command prompt to locate it. From memory (and because I can't be bothered to switch on my second computer) USB flash drives are not mounted on /dev but on /media

    Comment

    Working...