If you want a simple solution, you can read the file character by character using fgetc. Since there are no newlines in the file, just ignore quotation marks and move to the next index when you find a comma.

char names[6000][20]; // an array to store 6k names of max length 19
FILE * data = fopen("./022names.txt", "r");
int name_count = 0, current_name_ind = 0;
int c;

while ((c = fgetc(data)) != EOF) {
    if (c == ',') {
        names[name_count][current_name_ind] = '\0';
        current_name_ind = 0;
        ++name_count;
    } else if (c != '"') {
        names[name_count][current_name_ind] = c;
        ++current_name_ind;
    }
}
names[name_count][current_name_ind] = '\0';

fclose(data);
Answer from jackl on Stack Overflow
Top answer
1 of 3
2

If you want a simple solution, you can read the file character by character using fgetc. Since there are no newlines in the file, just ignore quotation marks and move to the next index when you find a comma.

char names[6000][20]; // an array to store 6k names of max length 19
FILE * data = fopen("./022names.txt", "r");
int name_count = 0, current_name_ind = 0;
int c;

while ((c = fgetc(data)) != EOF) {
    if (c == ',') {
        names[name_count][current_name_ind] = '\0';
        current_name_ind = 0;
        ++name_count;
    } else if (c != '"') {
        names[name_count][current_name_ind] = c;
        ++current_name_ind;
    }
}
names[name_count][current_name_ind] = '\0';

fclose(data);
2 of 3
2

"The code executes for the 1st iteration and names[0] contains the whole file...., How can I separate all the names?"

Regarding the first few statements:

char names[6000][20]; // an array to store 6k names of max length 19
FILE * data = fopen("./022names.txt", "r");

What if there are there are 6001 names. Or one of the names has more than 20 characters? Or what if there are way less than 6000 names?

The point is that with some effort to enumerate the tasks you have listed, and some time mapping out what information is needed to create the code that matches your criteria, you can create a better product: The following is derived from your post:

  • Process ascii files in c
  • Read file content that is separated by characters
  • input is a comma separated file, with other delimiters as well
  • Choose a method best suited to parse a file of variable size

As mentioned in the comments under your question there are ways to create your algorithms in such way as to flexibly allow for extra long names, or for a variable number of names. This can be done using a few C standard functions commonly used in parsing files. ( Although fscanf() has it place, it is not the best option for parsing file contents into array elements.)

The following approach performs the following steps to accomplish the user needs enumerated above

  • Read file to determine number of, and longest element
  • Create array sized to contain exact contents of file using count of elements and longest element using variable length array (VLA)
  • Create function to parse file contents into array. (using this technique of passing VLA as function argument.)

Following is a complete example of how to implement each of these, while breaking the tasks into functions when appropriate...

Note, code below was tested using the following input file:

names.txt

"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER",
"Joseph","Bart","Daniel","Stephan","Karen","Beth","Marcia",
"Calmazzothoulumus"

.

//Prototypes
int    count_names(const char *filename, size_t *count);
size_t filesize(const char *fn);
void   populateNames(const char *fn, int longest, char arr[][longest]);

char *filename = ".\\names.txt";

int main(void) 
{
    size_t count = 0;
    int longest = count_names(filename, &count);
    char names[count][longest+1];//VLA - See linked info
                                 // +1 is room for null termination
    memset(names, 0, sizeof names);
    populateNames(filename, longest+1, names);
            
    return 0;
}

//populate VLA with names in file
void populateNames(const char *fn, int longest, char names[][longest])
{
    char line[80] = {0};
    char *delim = "\",\n ";
    char *tok = NULL;
    FILE * fp = fopen(fn, "r");
    if(fp)
    {
        int i=0;
        while(fgets(line, sizeof line, fp))
        {
            tok = strtok(line, delim);
            while(tok)
            {
                strcpy(names[i], tok);
                tok = strtok(NULL, delim);
                i++;
            }
        }
        fclose(fp);
    }
}
    
//passes back count of tokens in file, and return longest token
int count_names(const char *filename, size_t *count)
{
    int len=0, lenKeep = 0;
    FILE *fp = fopen(filename, "r");
    if(fp)
    {
        char *tok = NULL;
        char *delim = "\",\n ";
        int cnt = 0;
        size_t fSize = filesize(filename);
        char *buf = calloc(fSize, 1);
        while(fgets(buf, fSize, fp)) //goes to newline for each get
        {
            tok = strtok(buf, delim);
            while(tok)
            {
                cnt++;
                len = strlen(tok);
                if(lenKeep < len) lenKeep = len;
                tok = strtok(NULL, delim);
            }
        }
        *count = cnt;
        fclose(fp);
        free(buf);
    }
    
    return lenKeep;
}

//return file size in bytes (binary read)
size_t filesize(const char *fn)
{
    size_t size = 0;
    FILE*fp = fopen(fn, "rb");
    if(fp)
    {
        fseek(fp, 0, SEEK_END); 
        size = ftell(fp); 
        fseek(fp, 0, SEEK_SET); 
        fclose(fp);
    }
    return size;
}
🌐
Wordpress
rakanalysis.wordpress.com › 2012 › 04 › 13 › fundamentals-of-ascii-file-inputoutput-in-c
Fundamentals of ASCII File Input/Output in C | A Technophile's Indulgence
October 26, 2014 - This corresponding function takes the contents of memory and for each mailbox slot, prints an integer of at most three significant digits to the file specified by the user. Empty mailbox slots are printed as zeroes to the output file. It should be noted that all of these functions only operate on ASCII text files, even with the formatted input.
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 89105-opening-ascii-files-c.html
Opening ASCII files in C?
April 25, 2007 - getc() and putc() are equivalent to fgetc() and fputc(), but are usually implemented as macros instead of functions; so the might be slightly faster, but don't pass them an argument like "c++", because it could be evaluated several times.
🌐
Edaboard
edaboard.com › eda software › software problems, hints and reviews
how to read ascii data in a file using c | Forum for Electronics
May 17, 2011 - Click to expand... Declare a file handle FILE *my_data; Use fopen to open file for reading my_data = fopen (my_filename, "r"); Then use fscanf to read data into predeclared variables fscanf (my_data, "%f %f %i %i", &f1, &f2, &i1, &i2); Then close the file using fclose fclose (my_data); Example of using these functions: fscanf() You can test the fscanf call in a while loop, when it returns null, end loop.
🌐
Stack Overflow
stackoverflow.com › questions › 43773659 › reading-an-ascii-file-in-c-using-fread
Reading an ASCII file in C using fread - Stack Overflow
Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I'm trying to read in an ascii file using fread(), bitwise complementing each byte and encoding it by adding 3 and writing out to a new file.
🌐
PCMAG
pcmag.com › home › encyclopedia
Definition of ASCII file | PCMag
A file that contains data made up of ASCII characters. It is essentially raw text just like the words you are reading now. Each byte in the file contains one character that conforms to the standard ASCII code (see ASCII chart).
Find elsewhere
🌐
GitHub
gist.github.com › fndari › 3f16496e5035a8a06ff8
Basic read and write with ASCII files with C++ · GitHub
Save fndari/3f16496e5035a8a06ff8 to your computer and use it in GitHub Desktop. Download ZIP · Basic read and write with ASCII files with C++ Raw · readfile.cxx · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
cppreference.com
en.cppreference.com › c › language › ascii
ASCII Chart - cppreference.com
June 20, 2021 - #include <stdio.h> int main(void) { puts("Printable ASCII:"); for (int i = 32; i < 127; ++i) { putchar(i); putchar(i % 16 == 15 ? '\n' : ' '); } } ... Printable ASCII: ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
🌐
Educative
educative.io › home › courses › learn c programming › input and output with files
Input and Output with Files
For ASCII files, there are functions to read and write single characters one at a time (getc and putc), perform formatted input and output (fscanf and fprintf), and read and write single lines at a time (fgets and fputs).
🌐
Stack Overflow
stackoverflow.com › questions › 52670078 › cannot-write-extended-ascii-to-file-in-c
Cannot write Extended ASCII to file in C - Stack Overflow
EDIT : the statement fprintf(f, "\u2588") works well to write into the file, but when reading the file and displaying it in the terminal the character is displayed as Γûê. In a nutshell, the written file accepts unicode happily, but not ascii 219...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › c program to find ascii value of a character
ASCII Value of a Character in C (3 Methods)
June 24, 2025 - Following this, you can then use the putc() function to write characters to the file. After this has been done successfully, you can then move on to the getc() function to read the file data and display the same on the console. To sum up, ASCII values play a significant role in C programming.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-ascii-value
ASCII Value of a Character in C - GeeksforGeeks
July 23, 2025 - Each character variable is assigned an ASCII value ranging from 0 to 127. ... Input: char ch = 'A'; Output: ASCII value of A is 65 Explanation: The character 'A' has an ASCII value of 65.
🌐
Programiz
programiz.com › c-programming › examples › ASCII-value-character
C Program to Find ASCII Value of a Character
In this program, the user is asked to enter a character. The character is stored in variable c. When %d format string is used, 71 (the ASCII value of G) is displayed.