How to read text in a file into a string variable in c

Converting a CSV record (a string) to an array is actually pretty easy. There are a couple of ways to do it, but using strtok() with a string→int function is perhaps the easiest.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  char s[1000];
  FILE * f = fopen("vectors.csv", "r");
  while (fgets(s, sizeof(s), f))
  {
    int vector[100];
    int size = 0;

    const char * ds = " ,\t\n";  // skip spaces, commas, tabs, and that newline at the end of s[]
    for (char * token = strtok(s, ds);  token;  token = strtok(NULL, ds))
    {
      vector[size++] = atoi(token);
    }

    do_something(vector, size);
  }
  fclose(f);
  return 0;
}

You may wish to add some error handling:

  • fopen() may return NULL
  • fgets() may not read the whole line (because there isn’t enough space in s[])
  • there may be more numbers per line than space available in vectors[]
  • atoi() may fail, but it won’t tell you if it does. strtol() is much better at error checking.

Notice also that your string→int function can be string→whatever: your vector could be composed of floating-point values, for example. You just need a function that will perform the conversion.

I have written it as a loop, assuming “vectorS” means that there are multiple records in your CSV, one vector per line, and that you wish to process all of them in turn. If your file is really just a single vector (on a single line) then just get rid of the loop.

If your file is massive to the point that using three passes over the string (fgets() plus strtok() plus atoi() / strtol() / scanf() / whatever) becomes an issue, you can do it in a single pass with just scanf(). Post a comment and I’ll update the answer.

Update

As your file contains nothing but numbers, separated by commas, as a single vector, you can also loop using just scanf().

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int vector[100];
  int size = 0;

  FILE * f = fopen("vectors.csv", "r");
  while (fscanf_s(f, "%d ,", vector + size) == 1)
  {
    size += 1;
  }
  fclose(f);

  do_something(vector, size);
}

While this looks very convenient, it cannot directly handle newlines in the input. If you were to wish to scan multiple vector records, one per line, you would still have to read the line in as a string, then parse the string. You can still do it with sscanf(), but it gets a tiny bit less easy to grok at first sight.

True single-pass parsing?

The update is a single-pass, but if you need to keep that (because you are trying to handle literally tens of thousands of values) and do it line-by-line, then you need to be a little sneakier when dealing with “delimiters”. Again, comment if it is an issue.

Vectors

In programming languages you can safely assume a vector some kind of list analogous to an array. The exact meaning of the term depends on either the language or the environment.

For example, vectors can be represented as tuples, arrays, lists, etc. In C++ a vector is a library object that represents a dynamic array. And so on.

In mathematical terms, a vector is a component of a linear space. But it is, again, often represented as a tuple of scalar values.