Question

I'm pretty new to C and thought I'd learn a bit more. I'm trying to write some code that reads a pre-existing text file and formats and writes it to disk. While it compiles, I get a segfault every time when I'm not sure I should be getting one. I went over most of the man pages and couldn't find anything that sticks out.

Here's the code:

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

int main()
{
   FILE *file = fopen("listing.txt","r"), *output;

   char *fvar;
   char *svar;
   char delim[] = ",";
   int num;
   char ch;
   char line[66];
   int listnum = 0;

   if(file == NULL){
        printf("Cannot open file.\n");
        exit(1);
   }

   output = fopen("report.txt","w");

   fprintf(output, "%s", "First Name     Last Name     Number ");
   fprintf(output, "%s", "-------------------------------------");

   while(fgets(line, 66, file) != NULL){
       ch = line[0];
       if((ch >= 'a') && (ch <= 'z')){
           fvar = strtok(line,delim);
           svar = strtok(NULL,delim);
           listnum++;
       }
       else {
            num = atoi(line);
       }
       fprintf(output, "%s", fvar);
       fprintf(output, "%15s", svar);
       fprintf(output, "%30d", num);
       fprintf(output, "%56s", "\n");
   }
   fclose(file); /* done reading from the input file */
   fclose(output); /* done writing the the output file */
   return 0;
}

What I'm trying to do is read a line of the text file. If the line contains info in the manner "string,string" then tokenize both of those and store them in fvar and svar respectively. If it is a numeric string, use atoi() to get the value and store it in num.

For some reason this causes a segfault, though it compiles fine. I'm pretty sure that the problem is in the lines fvar = strtok(temp,delim); and svar = strtok(NULL,delim);, but I don't know how to amend it. Note : the same behaviour occurs if I use fvar = strtok(line,delim);.

Edit: Fixed, thanks @Chris Dodd. As per my own volition, I will go fetch a first grade text book and study my alphabet again.

Was it helpful?

Solution

Most likely problem is that your first input line doesn't begin with a lower case letter, so you never call strtok in the first place, and never assign to either svar or fvar, but you still pass those (uninitialized) values to printf, which then gives a segfault...

Try using a debugger to single-step through the code to see where it actually goes.

OTHER TIPS

strtok can return NULL. If you ignore that possibility, access violations are easy to cause. always check the return status of a function you call.

fvar = strtok(temp, delim); 
if (fvar != NULL) 
{
    printf("%s\n", fvar);
    svar = strtok(NULL, delim);
    if (svar != NULL)
    {
        printf("%s\n", svar);
    }
    else
    {
        /* illegal to access contents of svar */
    } 
 } 
 else 
 {
     /* illegal to access contents of fvar */ 
 }

/* unrelated to the question, you may find the include file ctype.h useful, as it provides a family of functions of the form.... toupper, isletter, isdigit and things like that */

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top