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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:44:06+00:00 2026-05-30T12:44:06+00:00

I have this defensive-programming issue that I don’t really know how to solve. I

  • 0

I have this defensive-programming issue that I don’t really know how to solve.

I have this function that takes file path and size of table (rows / columns count) as arguments and I’m looking for better way of validating the input file. I assume that arguments of this function are always correct. size represents the “smaller side” of the table that is stored in file:

For example :

1 2 3 4 
5 6 7 8 

size = 2
is correct while

1 2 3 4 5
5 6 7 8 9

size = 2
is incorrect

I’d also like to be able to reject the files like this

1 2 3 4 5 6 7 8

size = 2 (which is accepted through fscanf)

Another type of file I’d like to be able to reject is

1 2 3
4 5 6

size = 2

As for now my only security is checking if the elements of the file are really numbers.

Here is the code I’ve done so far :

void import(float** table, int size, char* path)
{
    FILE* data = fopen(path, "r");
    assert(data);
    int i,j;
    int st;

    for (i=0; i<size; i++)
    {
        for(j=0; j<(size*2)-1; j++)
        {
            st = fscanf(data, "%f", &table[i][j]);
            if (!st)
            {
                printf("Error while importing the file.\n");
                fclose(data);
                return -1;
            }
        }
    }
    fclose(data);
}

I don’t really where and how to start, I’m not really proficient in C and there seems to exist a lot of functions and mechanisms to do what I want but they all seem very complex and some are actually longer than the code I provided.

If anyone can point me in the right direction that’d 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-05-30T12:44:07+00:00Added an answer on May 30, 2026 at 12:44 pm

    Your for loop could look like this:

    char line[1000], *token;
    for (i = 0; i < size; i++) // for each line
    {
        if (fgets(line, 1000, data) != NULL) // read line
        {
            token = strtok (line," ");
            for (j = 0; j < (size * 2) - 1; j++) // for each number from line
            {
                if (sscanf(token, "%f", &table[i][j]) <= 0)
                {
                    // there are columns missing:
                    printf("Error while importing the file.\n");
                    fclose(data);
                    return -1;
                }
                token = strtok (NULL," ");
            }
        }
        else
        {
            // there are rows missing:
            printf("Error while importing the file.\n");
            fclose(data);
            return -1;
        }
    }
    

    Also note that assert(data); should be replaced with something like this:

    if (!data)
    {
        printf("Error while openning the file [filePath=\"%s\"].\n", filePath);
        cleanExit();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code in jQuery, that I want to reimplement with the prototype
I have this RewriteRule that works too well :-) RewriteRule ^([^/]*)/$ /script.html?id=$1 [L] The
I have this line in a useful Bash script that I haven't managed to
Let's say we have a function that changes a password for a user in
I have this method for shrinking down an image for a website that I'm
I feel that my shop has a hole because we don't have a solid
This is a tooltip js function that I found on the internet and re-coded
So I have this fiddle which totally explains my issue. Working fiddle The JS
I have this idea for a free backup application. The largest problem I need
I have this gigantic ugly string: J0000000: Transaction A0001401 started on 8/22/2008 9:49:29 AM

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.