1 Replies - 1504 Views - Last Post: 05 October 2009 - 10:37 AM Rate Topic: -----

#1 dp9291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-October 09

segmentation fault (core dumped)

Post icon  Posted 05 October 2009 - 09:55 AM

I need help with two things. Can someone explain what this error actually means and also if you can help with fixing my code. this is the beginning of my project but so not sure if this is enough to work with or not. when i compile and run it the output is
Usage: executable input_file1 input_file2 output_file
Segmentation fault (core dumped)

The main thing I am trying to test right now is the last to lines with the fscanf and printf.

Thanks for all the help

#include <stdio.h>
#include <stdlib.h> // for statement exit(0)
  int main(int argc, char *argv[])
  {
	int numCalls[5];
	char phoneNumber[11];
	char city[19];
	char startTime[7];
	char endTime[7];
	char amPM[1];
	float cost[3];
	int i, areaCode, num1, num2;

	FILE *input, *output;
	char ch;
	if (argc != 3) // the program needs 2 more arguments besides
	  // the executable file name
	  { printf("Usage: executable input_file1 input_file2 output_file\n");
		//exit(0);
		  }
	input = fopen(argv[1], "r");
	// it is safe to check whether a file has been opened properly.
	if (input == NULL) // the file is not opened properly
	  { printf("File %s cannot open!\n", argv[1]);
		exit(0);
	  }
	output = fopen(argv[2], "w");
	if (output == NULL) // the file is not opened properly
	  { printf("File %s cannot open!\n", argv[2]);
		exit(0);
	  }
	/*for (; fscanf(input, "%c", &ch) != EOF; )
	  { fprintf(output, "%c", ch);
		printf("%c", ch);
		}*/
	//call index
	for(i=1; i<6; i++)
	  {
		printf("%d ", i);
	  }
	fscanf(input, "(%3d)%3d%4d \n", &ch, &areaCode, &ch, &num1, &num2);
	printf("%3d%3d%4d \n", areaCode, num1, num2);
		fclose(input);
		fclose(output);
		return 0;
  }



This post has been edited by dp9291: 05 October 2009 - 09:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: segmentation fault (core dumped)

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: segmentation fault (core dumped)

Posted 05 October 2009 - 10:37 AM

A seg fault is when the program tries to read or store data in an invalid manner.

Your fscanf() function has 3 "reads", but you have 5 things to be stored in. Also your first one "ch" is a single char, but fscanf is trying to read 3 integers into it.


edit: How is your input file formatted?

This post has been edited by KYA: 05 October 2009 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1