Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading a csv file in C

Status
Not open for further replies.

rmunny

Newbie level 6
Joined
Jul 21, 2017
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
143
Hello,
I have a .CSV file having around 1000 data. Those are the data collected from a 12 bit ADC. So they are like 4095, 2000, 1000, 1798, 3578 and so on. I need to read the file (Just for information I am using code composer studio) and store the data into array. So that I can utilize the data from the array for further processing. I got few C code online but due to lack of comment I don't understand how those work or beyond my level of understanding. Please help me sharing an example C code which has enough comment or any sugession how can I write a code by myself is highly appreciated.

Thanks
 

Hi,

I don´t have C code for you.

But it should be simple.
.CSV files contain lines. Each line is delemited with the "line delimiter".
Each line contains colums. The columns are divided with the use of "column delimiter".

Thus the most important for you to know is:
* what is the line delimiter (ASCII value)
* what is the column delimiter (ASCII vlaue)
* and how to interprete the data. Usally they are string values. Like "1.23" (this is a string of 4 ASCII values). Usually then you need to translate the string into a numeric value like 1.23 (this is a floating point value)

Look for video tutorials: "C# parsing csv file"

Klaus
 

hello,


i don't know on what systeme you will use a C Program

but wit a PIC MCU
i use this , to get RGB values, in a CSV file ( with coma separateur)
the main function used is strtok
must exist in C standard

Code:
  // Main loop
  do
  {

    if(UART1_DataReady==1)
    {
      UART1_Write_CText("Reception de : \r\n");
      UART1_Write_Text(buffer1); CRLF1();
      p1=0;
      //   strConstRamCpy(buffer1,"RGB1,123,245\r\n");
      p1=strstr(buffer1,"RGB");
       if (p1>0)
       {
          cc=*(p1+3);
          UART1_Write_CText("Led # ");UART1_Write(cc);
          CRLF1();
          p1=strtok(p1,",");  // 1er appel pour detection virgule
          UART1_Write_Text(p1);CRLF1();
          // RGB_Led[cc][0]=atoi(p1);
          Delay_ms(1000);

          i=0;
          do
          {   p1=strtok(0, ",");    // detection des separateurs (virgule) suivants
              if (p1!=0)
              {
              UART1_Write_CText("Parametre  ");
              WordToStr(i,CRam1);
              UART1_Write_Text(CRam1); UART1_Write(TAB);
              UART1_Write_Text(p1);
              ValColor=atoi(p1);
              IntToStr(ValColor,CRam1);
              UART1_Write_CText(" ValColor ");UART1_Write_CText(Colors[i]);
              UART1_Write(TAB); UART1_Write_Text(CRam1);
              CRLF1();
              Delay_ms(1000);
              i++;
              }
           }while (p1!=0) ;
           CRLF1();
           Delay_ms(1000);
        }
        strConstRamCpy(txt,"\n End of test \n");
        UART1_Write_Text(txt);
        CRLF1();
        RAZ_UART1(); // on nettoie tout
      } //if
    }
    while(1);
  }
 

I'm with Paulfjujo on this one.

If the file is simply a long list of comma separated numbers it might be better to read it byte by byte and look for the commas, moving to the next array position each time you find one.

If the file has lines with carriage returns or line feeds every so often, read it as a line of text and use 'strtok' to split it into component parts. If you use a comma as the delimiter it will return a variable each time it finds one so you can save it to the array and move on to the next one.

Brian.
 

I'm with Paulfjujo on this one.

If the file is simply a long list of comma separated numbers it might be better to read it byte by byte and look for the commas, moving to the next array position each time you find one.

If the file has lines with carriage returns or line feeds every so often, read it as a line of text and use 'strtok' to split it into component parts. If you use a comma as the delimiter it will return a variable each time it finds one so you can save it to the array and move on to the next one.

Brian.

Hello all,
Thanks for response. Sorry for not mentioning the system I am using. So I'm using MSP430F5438A board. Now just to make my cocept clear from the discussion above I wrote the code below. In this code I am writing to a file and then trying to read it. But here I find that the file is created but nothing is writtern in it so consequentially I can't able to read back anything! What am I doing wrong here:

Code:
#include [COLOR="#000000"]<[/COLOR]msp430.h>
#include [COLOR="#000000"]<[/COLOR]stdio.h>
#include [COLOR="#000000"]<[/COLOR]stdlib.h>
#include [COLOR="#000000"]<[/COLOR]string.h>


int main()
{
    WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  char *getline;
  FILE *fp;
  fp= fopen("testing.txt", "w");
  fputs("Writing", fp);
  fclose(fp);
fp= fopen("testing.csv", "r");
  fgets(getline, 8, fp);

  return 0;
}
 
Last edited by a moderator:

I think your problem is you put "Writing" in a file called "testing.txt" but then read it back from a file called "testing.csv".
You are trying to read from the wrong and possibly non-existent file!

Brian.
 

Hello Brian,
Sorry, actually, it was a typo. I was trying both CSV and txt file format. But while typing my code here I wrote it by mistake. I checked my code again but still not working. Any further sugession?

Thanks.
 

While the fclose should flush the buffer, try a fflush before the fclose.
Also check for error status returns form the functions - just blindly assuming they are working is not good fault-finding.
I am assuming that you are stepping through the code and examining the 'getline' (possibly an unfortunate name) array to know that nothing is written. Can you examine the file system by some other means on that board?
I had a quick look at the specs for the MSP430F5438A Experimenter's Board from TI (if that is the one you are using) and I can't see where it is storing files. There doesn't seem to be a SD card slot or USB Host port for a USB memory stick - perhaps I've missed it.
Where is the file being stored?
Susan
 

While the fclose should flush the buffer, try a fflush before the fclose.
Also check for error status returns form the functions - just blindly assuming they are working is not good fault-finding.
I am assuming that you are stepping through the code and examining the 'getline' (possibly an unfortunate name) array to know that nothing is written. Can you examine the file system by some other means on that board?
I had a quick look at the specs for the MSP430F5438A Experimenter's Board from TI (if that is the one you are using) and I can't see where it is storing files. There doesn't seem to be a SD card slot or USB Host port for a USB memory stick - perhaps I've missed it.
Where is the file being stored?
Susan

Hi Susan,
The file I am trying to write will store in my PC. I found the file in the debug folder which is inside the project directory I'm working. So I found a link below:
**broken link removed**
If you go to section 7.2.1 High-Level I/O Functions then you will find somthing similar I'm trying to do. Make my code exactly similar but not able to write in it.

Code:
#include <[COLOR="#000000"]m[/COLOR]sp430.h>
#include <[COLOR="#000000"]s[/COLOR]tdio.h>


void main()
{
    WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT

    FILE *fp;
    fp= fopen("testing", "w");
    fprintf(fp, "Writing\n");
    fclose(fp);

}
 
Last edited by a moderator:

The code snippet is fine, it just creates a file called "testing" in write mode, puts "Writing\n" in it then closes the file. As Susan pointed out, the fclose(fp) should flush the buffer automatically although in the case of a file with only a few bytes it would probably be saved in a buffer in the drive and a secondary mechanism would actually store it. Nothing needs to be done by the user beyond what you have already done.

I am a little puzzled about you saying the file is stored in the debug folder in the projects directory but it has the line "#include <msp430.h>< msp... >" in it. The include and the 'WDTCTL' suggests an MCU type so can you explain what type of PC this is or alternatively, how the file reaches the PC filing system.

I think it's time to see whether there is a writing or a reading problem. As you are running the program on a PC, can you show us the file size and contents using another program please. I'm not sure what operating system you are using but the file listing should show it's size and as it is text, you should be able to read it with any text editor or word processor.

Brian.</msp430.h>
 

Did you also read the 2nd 'Note' on the page before the document section you mentioned - the one entitled "C I/O Mysteriously Fails"?
What is the heap size setting? The default of 128 bytes might not be enough given what is written in Section 6.1.6 about the default size of BUFSIZ (256). Of course all this could be sorted out in the linker file or compiler options etc that we are not shown.
Susan

(Later Edit:)
Looking back I can see where some confusion might arise. This is posted in the Microcontrollers forum and mentions a MSP430F5438A board. However it would appear that you are simply running the MSP430 C/C+ compiler on your PC, presumably generating code for the PC and running the resulting code there - correct?
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top