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

  • SEARCH
  • Home
  • 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 6924865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:43:00+00:00 2026-05-27T10:43:00+00:00

I am trying to read from a file that has comma seperated values like

  • 0

I am trying to read from a file that has comma seperated values like so (5th field should only have r/b/n and 6th field is y/n:

531,A,10,10,b,n,96.00,100.00
531,B,15,15,b,n,144.00,0.00
531,C,20,20,b,n,192.00,0.00
533,A,11,11,r,n,123.20,100.00
533,B,22,22,r,n,246.40,0.00

I have the following fscanf code block to parse the file by line and store values in an array which I alter/output later.

while(fscanf(data, "%4[0-9],%[A-Z],%3[0-9],%3[0-9],[prb],[yn],%d,%d\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]) != EOF) {
   printf("%d,%c,%d,%d,%c,%c,%.2lf,%.2lf\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]);
   i++;
}

Unfortunately, I am getting a Segmentation fault and it occurs at this line. I have altered and tested it for about an hour and I am pretty sure I have it right, unless I am missing something. Any help would be appreciated, the full main function is pasted below.

main() {
   FILE *data;
   int house[200];
   char room[200];
   int length[200];
   int width[200];
   char paintcode[200];
   char ceilingcode[200];
   double cost[200];
   double setupcost[200];
   char line;
   int MAX_BUFF = 200;
   char x[200];
   double price[100];
   int i = 0, num_records = 0;

   data = fopen("quotes.data","r");
   if(data == NULL){
      printf("Error: file can't be open..\n");
   } else {
      while(fscanf(data, "%4[0-9],%[A-Z],%3[0-9],%3[0-9],[prb],[yn],%d,%d\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]) != EOF) {
         printf("%d,%c,%d,%d,%c,%c,%.2lf,%.2lf\n", &house[i], room[i], &length[i], &width[i], paintcode[i], ceilingcode[i], &cost[i], &setupcost[i]);
         i++;
      }
      fclose(data);
  • 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-05-27T10:43:00+00:00Added an answer on May 27, 2026 at 10:43 am

    There are quite a few issues here.

    For a start, the scanf family will only return EOF if reading failed without getting any input values. If you successfully get some but not all, you will get a count of the number successfully scanned (including the case where you got them all).

    You also need to pass the address of the variables you want to populate, which you haven’t done for all of them.

    In addition, a format specifier like %4[0-9] will read characters, not a single integer so it would require a character buffer, not an address of an integer as you have. If you want an integer populated, you need to use something like %4d.

    On the %[A-Z] front, these actually create a null-terminated string so you need to allow space for that.

    Also ensure you can read the the floating point values at the end of the line rather than integers, and you’re missing some % format characters.

    And lastly, printf requires addresses only for string arguments.

    The following program shows this in action in simplified form (no arrays) with your input file:

    #include <stdio.h>
    
    int main (void) {
        FILE *data;
        char room[2], paintcode[2], ceilingcode[2];
        int house, length, width;
        double cost, setupcost;
    
        data = fopen("quotes.data","r");
        if (data == NULL) {
            printf("Error: file can't be opened.\n");
        } else {
            while (fscanf (data, "%4d,%[A-Z],%3d,%3d,%[prb],%[yn],%lf,%lf\n",
                &house, room, &length, &width, paintcode, ceilingcode, &cost,
                &setupcost) == 8)
            {
                printf ("%d,%c,%d,%d,%c,%c,%.2lf,%.2lf\n",
                    house, *room, length, width, *paintcode, *ceilingcode,
                    cost, setupcost);
            }
        }
        fclose(data);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read strings from a file that has each string on
I'm trying to create a Zip file from .Net that can be read from
I am trying to read a binary file from a program that writes a
I am trying to read values from an input file in Perl. Input file
I'm trying to read a file from a local filesystem. I do not have
I am trying to load a csv file that has 14 columns like this:
I am trying to read in a file that has HTML list items with
I am trying to read a file that has been saved on the /system
I am reading a file that has 10,000 int values and then trying to
I'm trying to read a CSV with R from a URL that has auth

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.