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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:28:37+00:00 2026-06-13T12:28:37+00:00

#include <stdio.h> #include <stdlib.h> #include <string.h> const char* getfield(char* line, int num) { const

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

const char* getfield(char* line, int num) {
        const char* tok;
        for (tok = strtok(line, ",");
                tok && *tok;
                tok = strtok(NULL, ",\n"))
        {
            if (!--num)
                return tok;
        }
        return NULL;
    }

    int main()
    {
        FILE* stream = fopen("b.csv", "r");
        char line[1024];
        char *pstr;int num;
         const char* value;

        while (fgets(line, 1024, stream))
        {
            char* tmp = strdup(line);
        //printf("Field 3 would be %s\n", getfield(tmp, 3));    
        value=getfield(tmp, 3);
        num =strtol(value,&pstr,10);
        printf("Field 3 would be %d\n", num);
        // NOTE strtok clobbers tmp
            free(tmp);
        }
    }

/* b.csv

301,36,15
302,88,75

/ / my output

Field 3 would be 15
Field 3 would be 75

*/

issue is : /* b.csv

301,36,15
 302,88,
 ,,,34

if the the table is broken as above “strtok” returns NULL ,and so “strtol” gives “segfault”..how to resolve it ?

Here the main issue is if 2nd is not present, it treats 3rd as a second and gives segfault !! for example in a 3rd row of b.csv “,,,34” is there means 3rd value is present, but It behaves like “34” is a 1st value and 2nd and third are respectively NULL !!

  • 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-13T12:28:38+00:00Added an answer on June 13, 2026 at 12:28 pm

    Why can’t you just check the value that you get from getfield(tmp, 3); for NULL and not call strtol if NULL is returned? Another way to get around is make a static char* not_found = ""; in getfield and return address to it instead of NULL, then strtol will not segfault.

    UPDATE

    Since i found that strtok really is helpless in this situation i tried to write code that does the same with strchr:

        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
    
        char* getfield(char* line, int num) {
            char* tok = line;
            char* result;
            if (line)
            {
                do
                {
                    if (!--num)
                    {
                        tok = strchr(line, ',');
                        if (tok == NULL)
                        {
                            tok = &line[strlen(line)];
                        }
                        size_t fieldlen = tok - line;
                        if (fieldlen)
                        {
                            result = (char*)malloc(fieldlen+1);
                            result[fieldlen] = '\0';
                            strncpy(result, line, fieldlen);
                            return result;
                        }
                        else
                        {
                            break;
                        }
                    }
                    tok = strchr(line, ',');
                    line = tok + 1;
                } while (tok);
            }
            result = (char*)malloc(2);
            strcpy(result, "0");
            return result;
        }
    
        int main()
        {
            FILE* stream = fopen("b.csv", "r");
            char line[1024];
            char *pstr;int num;
            char* value;
    
            while (fgets(line, 1024, stream))
            {
                char* tmp = strdup(line);
                //printf("Field 3 would be %s\n", getfield(tmp, 3));    
                value=getfield(tmp, 3);
                num =strtol(value,&pstr,10);
                free(value);
                printf("Field 3 would be %d\n", num);
                // NOTE strtok clobbers tmp
                free(tmp);
            }
        }
    

    This worked on input file:

        10,,30
        10,
    

    The code returns 0 if nothing is found, you can change that, and the result is dynamically allocated. I hope this helps, the lesson for me is – avoid C when parsing strings 😀

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

Sidebar

Related Questions

#include<stdlib.h> #include<string.h> #include<stdio.h> int pstrcmp( char **p,char **q) { return strcmp(*p,*q) ; } int
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include<iostream> #include<stdlib.h> #include<string.h> #include<stdio.h> using namespace std; union type{ int a; char b; int
#include<stdio.h> #include<stdlib.h> char* re() { char *p = hello; return p; } int main()
#include <stdio.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc != 2) return
#include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<string.h> struct person *create_node(char *, char *,int); void addnode(char *,char *,int,struct
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *a = Hello ;
#include <stdio.h> #include <stdlib.h> #include <string.h> char * odd(const char * S, char *
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> static char *dup_str(const char *s) {

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.