Thread: reading file confusion

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    reading file confusion

    Code:
    int readParameterFile(parameters *p, int argc, const char *const *argv)
    {
          FILE *fp;
          if((fp=fopen(argv[1], "r")) == NULL) {goto ERROR1;}
          if (fp)
    	{
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->P)!=1){goto ERROR2;}
    	  fprintf(stderr, "P = %d\n", p->P);
    	  if(fscanf(fp,"P\t\t=\t%lf\n", &p->D)!=1){goto ERROR2;}
    	  fprintf(stderr, "D = %lf\n", p->D);
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->L)!=1){goto ERROR2;}
    	  fprintf(stderr, "L = %d\n", p->L);
    	  if(fscanf(fp,"P\t\t=\t%lf\n", &p->Tfinal)!=1){goto ERROR2;}
    	  fprintf(stderr, "Tfinal = %lf\n", p->Tfinal);
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->Trials)!=1){goto ERROR2;}
    	  fprintf(stderr, "Trials = %d\n", p->Trials);
    	  if(fscanf(fp,"P\t\t=\t%lf\n", &p->J)!=1){goto ERROR2;}
    	  fprintf(stderr, "J = %lf\n", p->J);
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->N)!=1){goto ERROR2;}
    	  fprintf(stderr, "N = %d\n", p->N);
    	  fclose(fp);
    	  
    	ERROR1:
    	  fprintf(stderr,"Failed to open parameter %s. Exiting.\n", argv[1]);
    	  return -1;
    	  
    	ERROR2:
    	  fprintf(stderr, "Error reading parameter file. Exiting.\n");
    	  return -1;
    	}
          else
    	{
    	  perror(argv[1]);
    	}
      return 0;
    }
    Code:
    Reading parameter file.
    Segmentation fault
    That's what I get from because I don't know what I'm supposed to do after ./filenameexe _______, ideally I want to enter a variable so that it would read it and use it to change the output...for example I wanna run ./filename.exe 10 and ./filename.exe 1 and see the difference in output.

    according to the code, is argv[1] supposed to be a filename entered at the command line

    The purpose of this function is that it grouped some pre-set parameters under struct parameters, and for it to read it from file straight rather than typing it at the command line. All i want to do is to change one variable, hence the argv[1] at the command line. How do I alter this code so that I can accomplish that?
    Last edited by vutek0328; 06-05-2007 at 04:21 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    argv[0] is the program name. All args from there on are ordered from 1 to argc-1.

    The code you posted is written quite poorly, since it uses goto in quite a bad manner. This is not the best way to use it, and really isn't defendable unless I'm missing something.

    I'm not sure about what you're trying to change. It doesn't sound like you wrote this code, and you seem kind of confused as to the end product. Could you please clarify exactly what you're trying to make this function do for you?

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Indeed I did not write this code, and I'm trying to decipher and take what I need out of it.

    I have a code with a number of pre-defined parameters, which are all included in the following header file(global.h):
    Code:
    #ifndef _GLOBAL
    #define _GLOBAL
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <gsl/gsl_rng.h>
    #include <gsl/gsl_randist.h>
    
    typedef struct parameters {
      int P; 
      int L; 
      int N;
      int Trials;
      double Tfinal; 
      double D;
      double J;
    } parameters;
    
    #endif
    Along with the file I posted above, they are both called by the main code.
    Code:
    #include "global.h"
    #include "io.h" /* the previous function */
    
    int main(int argc, char *argv[])
    {
     parameters p;
     
     fprintf(stderr, "Parsing command line.\n"); fflush(stderr);
     if(parseCommandLineArguments2(&p, argc, argv)!=0) {exit(-1);}
     fprintf(stderr,"Finished parsing command line.\n\n"); fflush(stderr); /*ignore this part, it is running just fine*/
     
     fprintf(stderr, "Reading parameter file.\n"); fflush(stderr);
     if(readParameterFile(&p)!=0) {exit(-1);}
     fprintf(stderr, "Finished reading parameter file.\n\n"); fflush(stderr);
         
     return 0;
    }
    Code:
    #include "io.h"
    
    int X;
    
    int parseCommandLineArguments2(parameters *p, int argc, char *argv[])
    {
      p->P=100;
      p->D=0.20;
      p->L=500;
      p->Tfinal = 300.0;
      p->Trials = 1;
      p->J=2.5;
      p->N=50;
      return 0;
    }
    
    int readParameterFile(parameters *p, int argc, const char *const *argv)
    {
          FILE *fp;
          if((fp=fopen(argv[1], "r")) == NULL) {goto ERROR1;}
          if(argc != 1)
    	{
    	  printf("Error!\n");
          return 0;
    	}
          else
    	{
    	  sscanf( argv[1], "&#37;d", &X);
    	}
          
          if (fp)
    	{
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->P)!=1){goto ERROR2;}
    	  fprintf(stderr, "P = %d\n", p->P);
    	  if(fscanf(fp,"P\t\t=\t%lf\n", &p->D)!=1){goto ERROR2;}
    	  fprintf(stderr, "D = %lf\n", p->D);
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->L)!=1){goto ERROR2;}
    	  fprintf(stderr, "L = %d\n", p->L);
    	  if(fscanf(fp,"P\t\t=\t%lf\n", &p->Tfinal)!=1){goto ERROR2;}
    	  fprintf(stderr, "Tfinal = %lf\n", p->Tfinal);
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->Trials)!=1){goto ERROR2;}
    	  fprintf(stderr, "Trials = %d\n", p->Trials);
    	  if(fscanf(fp,"P\t\t=\t%lf\n", &p->J)!=1){goto ERROR2;}
    	  fprintf(stderr, "J = %lf\n", p->J);
    	  if(fscanf(fp,"P\t\t=\t%d\n", &p->N)!=1){goto ERROR2;}
    	  fprintf(stderr, "N = %d\n", p->N);
    	  fclose(fp);
    	  
    	ERROR1:
    	  fprintf(stderr,"Failed to open parameter %s. Exiting.\n", argv[1]);
    	  return -1;
    	  
    	ERROR2:
    	  fprintf(stderr, "Error reading parameter file. Exiting.\n");
    	  return -1;
    	}
          else
    	{
    	  perror(argv[1]);
    	}
      return 0;
    }
    Essentially, I want to read the parameters in the struct and add ONE additional parameters which I will be changing in order to see the results of the output. The error is in the very first function I posted, how do I change that?

    Edit: Should this fix the problem of entering one additional variable (X)?
    Last edited by vutek0328; 06-05-2007 at 04:39 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(readParameterFile(&p)!=0)
    Passed 1 argument

    > int readParameterFile(parameters *p, int argc, const char *const *argv)
    Expects 3 arguments.

    1. How did this ever compile?
    2. How did this ever run for someone else?

    Make sure there are prototypes for all external functions in scope for when you try to call the function, and where you define the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    hehe, I already started to mess around with the code so that I can use it, but thanks for the reminder Salem

    How many arguments should I pass there? I'm guessing 3: parameter pointer and an additional variable(argc and argv)?

    Edit: nevermind, fixed it
    Last edited by vutek0328; 06-06-2007 at 11:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM