Results 1 to 8 of 8

Thread: Read a CSV file from a flash drive (Ubuntu 12.04)

  1. #1
    Join Date
    Mar 2013
    Beans
    3

    Read a CSV file from a flash drive (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.


    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 lisati; March 26th, 2013 at 11:54 AM. Reason: Added [code] and [/code] to aid readbility

  2. #2
    Join Date
    Aug 2007
    Location
    From the land down under
    Beans
    1,243
    Distro
    Ubuntu Development Release

    Re: Read a CSV file from a flash drive (Ubuntu 12.04)

    You may have more luck in the Programming section, so I've moved it there for you.
    // Blog

  3. #3
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Read a CSV file from a flash drive (Ubuntu 12.04)

    Quote Originally Posted by robotarduino View Post
    fd = fopen("/dev/sdc/test.csv","r");
    /dev/sdc is device name. You cannot open files on the device this way. The mount point is probably located under /media
    Last edited by schragge; March 26th, 2013 at 11:31 AM.

  4. #4
    Join Date
    Mar 2013
    Beans
    3

    Re: Read a CSV file from a flash drive (Ubuntu 12.04)

    How can I figure out what is my mount point?

  5. #5
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Read a CSV file from a flash drive (Ubuntu 12.04)

    From inside C code?

    http://stackoverflow.com/questions/9...t-mount-points
    http://stackoverflow.com/questions/8...-device-in-c-c

    From the command line?
    Code:
    findmnt -notarget /dev/sdc1

  6. #6
    Join Date
    Mar 2013
    Beans
    3

    Re: Read a CSV file from a flash drive (Ubuntu 12.04)

    Hey, thanks much! that worked!. However, is there a way to read a file without knowing the mount point? Every time I used a different flash drive the mount point is different, I just need to read a file from a USB port without knowing the name of the flash drive.

  7. #7
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Read a CSV file from a flash drive (Ubuntu 12.04)

    You don't want to rely on device names like /dev/sdc1, do you? Your computer probably has several USB ports, and there may be different number of USB devices attached at any given time. You cannot reliably predict which device name kernel will assign to your flash drive next time. That's why you usually specify devices either by label or by UUID. IIRC, removable flash drives usually get mounted under /media/, the last part being either the label (if the drive has one) or the UUID.

    From the command line, you can list all plugged in removable drives and their mountpoints (if they are mounted) with
    Code:
    lsblk -nrorm,type,name,mountpoint|sed -n 's/^1 part //p'
    Alternatively, you can manually check the contents of file /sys/block/*/removable for each device listed under /sys/block/.

  8. #8
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,165
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Read a CSV file from a flash drive (Ubuntu 12.04)

    Quote Originally Posted by robotarduino View Post
    Hey, thanks much! that worked!. However, is there a way to read a file without knowing the mount point? Every time I used a different flash drive the mount point is different, I just need to read a file from a USB port without knowing the name of the flash drive.
    Maybe you can just search the file under /media...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •