Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:13:34+00:00 2026-06-14T09:13:34+00:00

I have a very simple problem. I need to read in the contents of

  • 0

I have a very simple problem. I need to read in the contents of a file into a char array in C. The file will always be formatted as two columns of letters, e.g.:

A B
B C
E X
C D

Each letter represents a vertex on a graph, which I’ll be dealing with later. I’ve learned programming using C++ and Java, but I’m not extraordinarily familiar with C specifically.

What’s causing the problem that I can’t figure out is the fact that the file is multiple lines long. I need each letter to occupy a slot in the array, so in this case it’d be:
array[0] = 'A', array[1] = 'B', array[2] = 'B', array[3] = 'C' and so on.

Eventually I’ll need the array to not include duplicates, but I can handle that later. I wrote a program earlier this semester that read in a single line of ints from a file and it worked fine, so I copied most of that code, but it’s not working in this case. Here’s what I have so far:

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

int main (int argc, char *argv[])
{
   int i;
   int count = 0;
   char * vertexArray;
   char ch = '\0';

// open file
   FILE *file = fopen( argv[1], "r" );

// count number of lines in file
   while  ((ch=fgetc(file)) != EOF)
        if(ch == '\n') count++;

// numbers of vertices is twice the number of lines
   int size = count*2;

// declare vertex array
    vertexArray = (char*) calloc(size, sizeof(char));

// read in the file to the array
   for(i=0; i<size; i++)
        fscanf(file, "%c", &vertexArray[i]);

// print the array
   for(i=0; i<size; i++)
        printf("%c\n", vertexArray[i]);

   fclose( file );
}

I know I need to be testing for the file opening and being read properly and whatnot, but I’ll add that in later. Just trying to read in the array for now. My output in this case is 8 blank lines. Any help would be great!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:13:35+00:00Added an answer on June 14, 2026 at 9:13 am

    When you loop through the file to count the number of lines, the file pointer is already sitting at EOF, so you won’t be reading anything into your array. At best it’ll be the same value as your last character, but it’ll probably show you a segmentation fault.

    What you want to do is

    rewind(file);
    

    before you

    //read file into the array
    

    then you’ll be starting at the beginning of the file.
    Also, I’m not sure how fgetc handles end of lines, since there’s usually a trailing '\0' sitting there. Another way you could do this is to use fscanf like so

    i = 0;
    while (!feof(file)){
       fscanf(file, "%c %c", &vertexArray[i], &vertexArray[i+1]);
       i++;
    }
    

    The function feof(FILE *) checks if the EOF flag has been set, and stops once you hit EOF.

    UPDATE: I think if you define ch as int ch, it should work. Have a look at this thread.

    This is the code that worked for me :

    int main (int argc, char *argv[])
    {
        int i;
        int count = 0;
        char * vertexArray;
        int ch, size;
    
        FILE *file;
        file = fopen(argv[1], "r");
    
        while ((ch = fgetc(file) != EOF))
           count++;
    
        size = count*2;
        vertexArray = (char*) calloc(size, sizeof(char));
        rewind(file);
    
        for(i=0; i<size; i++)
           fscanf(file, "%c ", &vertexArray[i]);
    
        for(i=0; i<size; i++)
           fprintf(stderr, "%c\n", vertexArray[i]);
    
        fclose(file);
    

    }

    NEW EDIT Notice the space after fscanf(file, "%c ", &vertexArray[i]);. That tells C that you want to skip all the white spaces after reading the character. Without the space, it’ll read the whitespace as a character as well. This should fix it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a big problem with very simple code. I need to get a
I have a very simple problem but cannot find a nice solution. I have
I have a very simple problem, but I can't seem to solve it. I
I have a very simple problem. I have a data frame with multiple rows
I have a very simple C# DataTable problem that I cannot seem to wrap
I have a very simple JavaScript/jquery code which won't just work correctly. The problem
I have made a very simple AppleScript to close tabs in Safari. The problem
I have a problem with OpenGl ES 2.0. I've designed a very simple framework
very new to signalR, and have rolled up a very simple app that will
I have very simple piece of code. The goal is when i input four-digit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.