0

Good day. Don't know whether this question has been asked before. Any who, I have a text file with contents like below

AP0003;Football;13.50;90
AP0004;Skateboard;49.90;30

It is basically,

Item Code;Item Name;Price per unit;Quantity

I am trying to put the contents of the text file into an array but I've had no luck so far. And, I can't find anything similar on Stack Overflow (or maybe my search parameters is not accurate). Would appreciate any help I can get. Am new to C Programming.

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
  • Looks like your file is in CSV format. You need a CSV parsing library. See for example: https://stackoverflow.com/questions/7827274/whats-the-preferred-library-for-csv-parsing-writing-in-c and all its duplicates. – fouronnes Jun 02 '15 at 10:36
  • I would eventually need to run my code on a Linux server. A CSV parsing library should work on it too right ? – Tan Yi Qian Jun 02 '15 at 10:40
  • If the library is applicable for -nix systems you should be able to run it on a linux machine as well. Otherwise, if you access to the sources, you just recompile it on the linux machine. – ckruczek Jun 02 '15 at 10:56

2 Answers2

2

Firstly open the file using fopen:

FILE* fp = fopen("NAME_OF_FILE.txt", "r"); // "r" stands for reading

Now, check if it opened

if(fp == NULL)                             //If fopen failed
{
    printf("fopen failed to open the file\n");
    exit(-1);                              //Exit program
}

Suppose that these are your arrays to store the line and each data are:

char line[2048];                          //To store the each line
char itemCode[50]; 
char item[50];
double price;
int quantity;                             //Variables to store data

Read the file using fgets. It consumes line by line. Put it in a loop which terminates when fgets returns NULL to scan the whole file line by line. Then extract data from the scanned line using sscanf. It, in this case, will return 4 if successful:

while(fgets(line, sizeof(line), fp) != NULL) //while fgets does not fail to scan a line
{
    if(sscanf(line, "%[^;];%[^;];%lf;%d", itemCode, item, price, quantity) != 4) //If sscanf failed to scan everything from the scanned line
            //%[^;] scans everything until a ';'
            //%lf scans a double
            //%d scans an int
            //Better to use `"%49[^;];%49[^;];%lf;%d"` to prevent buffer overflows
    {     
         printf("Bad line detected\n");
         exit(-1);                          //Exit the program
    }
    printf("ItemCode=%s\n", itemCode);
    printf("Item=%s\n", item);
    printf("price=%f\n", price);
    printf("Quantity=%d\n\n", quantity);    //Print scanned items
}

Finally, close the file using fclose:

fclose(fp);
Spikatrix
  • 20,225
  • 7
  • 37
  • 83
1

You can try this code :

#include <stdio.h>
#include <stdlib.h>
int main() 
{
 char str1[1000],ch;
 int i=0;
 FILE *fp;
 fp = fopen ("file.txt", "r"); //name of the file is file.txt
 while(1)
   {
    fscanf(fp,"%c",&ch);  
    if(ch==EOF) break;   //end of file
    else str[i++]=ch;    //put it in an array
    }    
 fclose(fp);   
 return(0);
}

This will put your entire file into an array str including '\n' and other special characters.If you dont want the special characters put neccessary conditions in the while loop.

Sourav Kanta
  • 2,727
  • 1
  • 18
  • 29