C, using fscanf


 
Thread Tools Search this Thread
Top Forums Programming C, using fscanf
# 1  
Old 04-29-2009
C, using fscanf

hey guys, I'm working on a term project for a c/unix class. The basis of the program is that it will calculate the weight/balance of a plane. I'm hoping to have an input file as such:

"
Pilot weight:
Passenger weight:
baggage wieght:


etc
"


and the idea is that I'm trying to use fscanf to read it line by line, baisically untill it hits the '\n' on the line. attached is the code I've got so far. my hope is that after its hit the '\n' that it will promt the user for a weight, and store that to an array. This i can figure out and can probably get done. However, my code as is is having issues. The menu will come up fine, but when i make a choice, i get 'segmentation fault' Now i've already asked my prof for help, and tried what he told me to do...and it's not helping.

I haven't got the print and calculate data functions made up yet, so no worries.

any assistance would be great! thanks

-j

Code:
/* this program will calculate the wieght and ballance of a plane before takeoff. It considres all of the possible
 * weight variables and will tell the user the location of the CG and the total weight at the end of the program.
 * CSC 218
 * Spring 2009
 * instructor: Dr. Manki min
 * Term Project
 * Name: John Langholz
 * ID: 1890473
 * due: 5/1/09
 */



#include<stdio.h>

void menu (int* choice);
int ac (int choice, FILE* plane);
void getdata (FILE* plane);
void calc (int* weight, int* balance);


int main (void)
{

  int choice;
  FILE* plane;

  menu(&choice);
  
  ac(choice, plane);  // ac or a/c is short for Aircraft. Easier separation between function and file called plane

  getdata(plane);


  return 0;

}


//=================================menu function================================


void menu (int* choice)
{
  printf("****************WELCOME!****************");
  printf("\n");
  printf("This program enabels you to calculate\n");
  printf("your weight and balance. Please enter\n");
  printf("a number and key <retrun> to make your\n");
  printf("selection from the menu below:\n");
  printf("\n");
  printf("1| Cessna 172\n");
  printf("2| Cherokee 6\n");
  printf("3| Piper Cub\n");
  scanf("%d", choice);

 return;
}


//=====================================select data=========================

int ac (int choice, FILE* plane)
{
  

 switch (choice)
     {
    case 1: plane =  fopen("C172.DAT", "r");
        break;
    case 2: plane =  fopen("Piper6.DAT", "r");
        break;
    case 3: plane =  fopen("Cub.DAT", "r");
        break;
      }
  


}
//============================================getting data===============


void getdata (FILE* plane)
{
  char charin;


  if (fscanf(plane, "%c", &charin), charin != '\n')
    printf("%d", charin);
  else
   //scan user input using array[i]
 


  return;
}



//===================calc data=========================

/*
 * calcuate the weight and balance, use the ary[i] to retireve the stored data and calculate using arm & placement
 */


//=========================print data======================

# 2  
Old 04-29-2009
fscanf reads a line from a file up to, but not including, the newline, and applies the format string to it (which works just like the one for printf). Meaning there's no need for checking for a newline or reading character by character (fgetc is better suited for that anyways)

Also, I don't think your segfault problem is really related to your menu. Try stepping though with a debugger (gdb, ddd, ...)

And I hate to be the one who has to tell you, but homework questions are forbidden by the rules, and you had to accept them when you registered your account.

P.S.: Asking for help 2 days before it's due might be a bit short sighted...
# 3  
Old 04-29-2009
I think given that you went through your professor first before coming here warrants some kind of leniency, but that's just my opinion, and I'm still new here.

In general, real programmers (tm) use "readline" and avoid scanf functions. The main problem with the scanf functions is their inherent lack of efficiency and security,oh -- and buffer overflows. To do character-by-character, one usually uses fgetc(). Search for tips on using readline/strtok. If the prof is making you do character-by-character, then use fgetc to build up your character array.

Last edited by otheus; 04-29-2009 at 07:47 AM.. Reason: efficiency is primary
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Fscanf to get number and replace.

Hi, I have a file with contents like "abcd 1234" .What i need is get that integer and replace that with 0. So i used fscanf(fp,"%s %d", str, &num); This is having some problem. There can be multiple space/tab between string and number. How to replace that number with 0 in same file? (4 Replies)
Discussion started by: explore
4 Replies

2. UNIX for Dummies Questions & Answers

how fscanf every two data

hi there... i have a question regarding the fscanf function... let's say i have a data: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 how do i read only the first COLUMN, or the second COLUMN or the third COLUMN or etc....?????? thanks (1 Reply)
Discussion started by: theunknown
1 Replies

3. Programming

fscanf: read words from file

Hi I have a file like that: 1 2 3 4 5 6 7 8 and I want print on stdout: 1 3 8 in other words i want choose what print out. I was thinking to use fscanf as: fscanf(file_in,"%d %d %d",&a, &b,&c); but in this way i get: 1 2 3 Is there a solution using fscanf to obtain my... (2 Replies)
Discussion started by: Dedalus
2 Replies

4. Programming

read a file wich fscanf() in a function

I use fopen, fscanf, fclose to read a file. It can work well. since many files should be read, a function is created with the same code. But in the function, fscanf can not work well. for example, the first line of the the file is: > filename but the fscanf will give: 207/23/eee/34 it appears... (2 Replies)
Discussion started by: cdbug
2 Replies

5. Programming

fscanf

Hi, Can any one tell me what "%hx" as control parameter mean in fscanf. Thanks, abey (4 Replies)
Discussion started by: abey
4 Replies

6. Programming

fscanf()

I keep trying to use fscanf() and for some reason I can't get the syntax down and always get seg faults. I'm on a SunOS 5.5.1, and my current code looks like this: int n1, n2, n3, n4, n5, n6; /* open config file */ if (fileptr = fopen(filename,"r") == NULL) { printf("couldn't open... (3 Replies)
Discussion started by: j_t_kim
3 Replies

7. Programming

fscanf()

thanks to everyone for your earlier replies, but i have yet another problem with file i/o. i'm trying to read multiple lines with the same file, and have been using the following code to take in the first two lines from a file... fscanf(fileptr, "%d %d %d %d %d %d\n", &n1, &n2, &n3, &n4, &n5,... (1 Reply)
Discussion started by: j_t_kim
1 Replies
Login or Register to Ask a Question