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.

#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;
}
You're hardcoding the file name in the program. It's shouldn't really matter where it is, unless that's the problem your trying to solve. On this box, the USB port maps onto /dev/sdb, so hardcoding these things isn't quite right.

CSV files are text files, so you really should use the ANSI file functions (fopen, ...) instead of the POSIX unbiffered file I/O that your using for C, or the C++ I/O stream library for C++.
"/dev/sda/", "/dev/sdb" and so on are raw devices. They never denote a file system but the underlying hard drive. To reference the file system, your USB device denoted by your raw device has to be mounted on any directory of your file system. F.e. if "/dev/sdc" was mounted on "/mnt/USB-Drive" (or whatever directory name you'll use) then you may reference your CSV file as "/mnt/USB-Drive/test.csv" (if the CSV file resides on the top level directory of your USB devices filesystem.
Topic archived. No new replies allowed.