Z-trooper
BF2s' little helper
+209|7229|Denmark
I am currently working on a rather small project for my examine in "C programming" this semester...

My bad luck haunts me I forgot my book on "C programming" today, not thats its critical to finish my project today its just that I finally found some time to write on my program..

I have to make a "survey system" program that will save data and is able to read the data and then sort it by input from the user..

So i figured that I can save replies on one line, comma separated..
I'm planning to read the comma separated data into an array and work with that array..

Any of you have any tips on how to achieve this?

Thanks in advance
ReTox
Member
+100|6969|State of RETOXification
What C?

C, C++ (ANSI), C++ MFC, Managed C++ (.NET), or C#?

A CSV is a really simple file to parse, just recurse through each line looking for commas and load it into an array:

Code:

     - Open File for Text Reading.
     - Loop through to find the line count (Y) and a count for the line with the most commas (X).
       The X and Y value are now your Array bounds.
     - Create an array (rows = lines of csv Y, cols = all the values per line X)

     int lastCharIndex := 0; // Global for position of start of word
     int colIndex := 0; // Global for knowing position in columns
     for (int x:=0; x<=TextFileLinesCount-1;x++) // Loop through all the lines in the file
     {
          string tmpString := csvFileHandleLines[x]; // Get a line of text from the file.
          int perStringIndex := 0; // Dirty way to monitor position of (int j) outside of the loop
          for (int y:=0;y<=tmpStringLength-1;y++) // Loop through each character to find commas
          {
               perStringIndex := y;
               if (tmpString[y] == ",")
               {
                    csvArray[x,colIndex] := substring(tmpString, lastCharIndex, y-1); // Add value to the array
                    lastCharIndex := y+1; // Set to start of next word (Y index char is a comma so we +1);
                    colIndex++;
               }
          }
          csvArray[x,colIndex] := substring(tmpString, perStringIndex+1,tmpStringLength-1); // Get Last Value 
          colIndex := 0; // Reset column index for next line
     }

     - Use your Array with whatever you want.
That's psuedo-code off the top of my head but it should get you going.  I recommend a lot of range and bounds checking!  I opted for X and Y for my loop variable to represent the row and column of the array.  I find it better to use X,Y,Z for arrays... easier to visualize the data when you think of it as a sheet or a cube in the Cartesian coordinate system.
Z-trooper
BF2s' little helper
+209|7229|Denmark
cool, look good mate

and its just good ol' regular "C", not all that fancy stuff

+1 for the help..
Z-trooper
BF2s' little helper
+209|7229|Denmark
Not that anyone really cares... I made a program that will create a new line instead of a comma... now that I have cracked that problem I can move on and load the data into a struct..

Code:

#include <stdio.h>

int main(void)
{

int n,c;
    
    n = 0;
    FILE *fp;
    fp = fopen("test.txt", "r"); 

        while(c = fgetc( fp)) { 
            if (feof(fp)) 
            break;
            if (c != ',')
            printf("%c", c); 
            if( c == ',' ) 
                printf("\n");
        }
                       fclose(fp); 
        return 0;
                    
       }
that is all...

Board footer

Privacy Policy - © 2025 Jeff Minard