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 6120295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:39:08+00:00 2026-05-23T15:39:08+00:00

I don’t work with C often so please excuse any mistakes I might be

  • 0

I don’t work with C often so please excuse any mistakes I might be making in terms of coding style 😛 I’m currently getting an error that I’m a bit stumped on: when I include the line tokenCopy = malloc(sizeof(fileSize));, I get random a random exclamation about 1/4th of the way through the output of a file to std but if the line is removed/commented, the data displays as expected:

MAY      +1.32      D1      1002
JUNE     -1.57      D3      201
JULY      -2.37      D4      478
AUGUST      +5.03      D2      930
SEPTEMBER      -3.00      D1      370
OCTOBER      +7.69      D1      112

and the actual output I get when the line is in place:

MAY      +1.32      D1      1002
JUNE      -1.57      D3      2!

and the relevant code:

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


    /**
     * Machine struct - three column
     **/

    /**
     * Parses the input file, size is the size of name
     **/

    char parseInputFile(char *name, int size) {
            FILE *fp;

          if ((fp = fopen(name,"r")) == NULL) {
                printf("Cannot open file %s\n", name);
                return 1;
            }
            else {
                int fileSize;
                fileSize = 0;
                char *fileContent;
                char *processedFileContent;

                //get file size
                fseek(fp, 0, SEEK_END);
                fileSize = ftell(fp);
                fseek(fp, 0, SEEK_SET);

                //allocate
                fileContent = malloc(sizeof(fileSize));
                processedFileContent = malloc(sizeof(fileSize));

                //read
                char c;
                int g;
                g=0;

                while((c = getc(fp)) != EOF) {
                    fileContent[g] = c;
                    g++;
                }

                //process
                char delim[6] = "      ";
                char *tokenCopy;
                tokenCopy = malloc(sizeof(fileSize));

                strcpy(tokenCopy, fileContent);
                char *tokens = strtok(tokenCopy, delim);

                while (tokens) {
                    tokens = strtok(NULL, delim);
                }

                puts(fileContent);
                //printf("File Size: %i \n",fileSize);
                //puts(tokenCopy);
                return *processedFileContent;
            }
    }

    int main(int argc, char *argv[])
    {
            //char *input;
            if (argc == 1)
                puts("You must enter a filename");
            else {
                int size = sizeof(argv[1]);
                parseInputFile(argv[1],size);
            }
            return 0;
    }

Could anyone offer any insight into what I’m doing wrong (or if my code is causing problems in itself)?

  • 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-23T15:39:08+00:00Added an answer on May 23, 2026 at 3:39 pm

    You put the size of the file in fileSize but then allocate only the space to store an int that is what sizeof(FileSize) will give you.

    These two lines

                fileContent = malloc(sizeof(fileSize));
                processedFileContent = malloc(sizeof(fileSize));
    

    should be (assuming you will treat the text you’ll read as a string):

                fileContent = malloc(fileSize+1);
                processedFileContent = malloc(fileSize+1)
    

    and, after having read the file content, you should put a ‘\0’ at the end.

    That said, I really don’t get what you are trying to achieve by using strtok(). If you only need to separate the three components of each line, you can do it much easily while you read the file since you read it one character at the time.

    If you elaborate a little bit more on what you’re trying to achieve, we might have other advice.

    UPDATE AFTER COMMENT BELOW

    You should step back a second and reconsider your problem as I suspect you don’t need to store any string at all. The first value is a month name, which can be stored as an integer, the second is a double (or float), the third seems ‘Dx‘ with x varying from 1 to 4, again this could be an integer. It seems the name of a sensor, so I suspect it could be coded in an integer anyway as there will surely be a finite number of them. And the fourth is clearly another integer.

    With a wild guess on what those fields mean, your struct would look like something like this:

    struct val {
       int month;
       double value;
       int sensor;
       int var;
    }
    

    Now, you can get the values as you go one char at the time, or read an entire line and get the values from there.

    Going one char at the time will not require any additional space but will result in a longer program (full of ‘if’ and ‘while’). Reading the line will be slightly easier but will require you to handle the maximum size of a line.

    A proper structuring of functions will help you a lot:

     do {
       if ((c = get_month(fp, &month)) != EOF) 
         if ((c = get_value(fp, &value)) != EOF)
           if ((c = get_sensor(fp, &sensor)) != EOF)
             if ((c = get_var(fp, &var)) != EOF)
               measures = add_data(measures, month, value, sensor, var);
     } while (c != EOF);
     return measures
    

    Where measures can be a linked list or a resizable array of your structs and assuming you’ll go one char at the time.

    There are quite many other details you should set before you’re done, I hope this will help you find the right direction.

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

Sidebar

Related Questions

Don't be afraid to use any technical jargon or low-level explanations for things, please.
I don't edit CSS very often, and almost every time I need to go
I don't currently use ajax.net though I would be open to it if it
Don't ask why, but is there any way to suppress a failed linking error?
I don't have any experience in updating a Rails app and when I google
I don't know when to add to a dataset a tableadapter or a query
I don't understand where the extra bits are coming from in this article about
I don't want PHP errors to display /html, but I want them to display
I don’t think I’ve grokked currying yet. I understand what it does, and how
I don't remember whether I was dreaming or not but I seem to recall

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.