handling input and output data in files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raindeer
    New Member
    • Mar 2007
    • 6

    handling input and output data in files

    Hello,

    I want to create an input file for my program in which I store all of my input data (constants etc.).
    Something of this kind:

    ------------
    Input parameters
    ------------

    boxlengthX 1.0
    boxlengthY 1.0
    boxlengthZ 1.0
    radius (nm) 10
    Temperature (K) 298
    etc.


    which scan function allows the various functions of the program to go and look for the necessary input from this file.
    And what function should I use to get the calculated values (outcome of several functions in the program) in a nicely ordered output file?

    I have been using scanf before but I don't think that will do the trick here. If you could just point out a better function, that would be a great help.

    Thank you very much!

    Tom
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    scanf() should work.

    If a file item looks like:

    boxlengthX 1.0

    then
    Code:
    scanf("%s%f", xxx,yyy);
    should fetch the string followed by the double. Just be careful that your strings do not have embedded spaces since scanf() stops on whitespace.

    You can easily use scanf() as long as you know the format of the inout file.

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by weaknessforcats
      scanf() should work.

      If a file item looks like:

      boxlengthX 1.0

      then
      Code:
      scanf("%s%f", xxx,yyy);
      should fetch the string followed by the double. Just be careful that your strings do not have embedded spaces since scanf() stops on whitespace.

      You can easily use scanf() as long as you know the format of the inout file.
      scanf() can be a pain in the ass. As you can see here (or will see after reading this), it can be easily used incorrectly (sorry weakness). Buffer overruns are possible to do as well. I'm going to have to write a tutoral for scanf at some point.

      The error in the code is that yyy (I'm assuming that it is defined as a double?) is not passing the address of the double, and the format string is incorrect. To correctly do what weakness stated is:
      Code:
      char xxx[100];
      double yyy;
      scanf("%s%lf", xxx, &yyy);
      To safely do it you would ensure you are not overruning the buffer:
      Code:
      char xxx[100];
      xxx[99] = '\0'; // or just assign xxx[100] = ""; and it will clear the entire array.
      double yyy;
      scanf("%99s%lf", xxx, &yyy);
      To ensure you have read in all of the data:
      Code:
      char xxx[100];
      xxx[99] = '\0'; // or just assign xxx[100] = ""; and it will clear the entire array.
      double yyy;
      if(2 == scanf("%99s%lf", xxx, &yyy)) {
        // read in both string and double
      }
      else {
        // not read in both string and double
      }
      I've sometimes had trouble with doing that last one (I think it caused a partial parse under certain circumstances, but I can't be sure), so I usually do this instead:
      Code:
      int parsedTo = 0;
      char xxx[100];
      xxx[99] = '\0'; // or just assign xxx[100] = ""; and it will clear the entire array.
      double yyy;
      scanf("%99s%lf%n", xxx, &yyy, &parsedTo)
      if (parsedTo != 0) {
        // read in both string and double
      }
      else {
        // not read in both string and double
      }
      Hope this helps.


      Adrian

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        scanf("%s%f", xxx,yyy);
        Sorry for the confusion here. I intended xxx and yyy to mean "appropriat e arguments". I'll be more careful in the future.

        Comment

        • raindeer
          New Member
          • Mar 2007
          • 6

          #5
          Thank you for your help!

          I now use getline to read in every line one by one. But how do I evaluate the value of the first part of the line? For instance, if the first part is "Temperatur e", how do I put the second part of the line (the actual numerical value of the parameter) in a variable "B".

          Code:
          double B;
          char xxx[100];
          variable[99] = '\0'; 
          /* or just assign xxx[100] = ""; and it will clear the entire array.*/
          double value;
          scanf("%99s%lf", xxx, &yyy);
          
          if (xxx == "Temperature")
          	B = value;
          will not do anything

          Tom
          Last edited by AdrianH; May 9 '07, 03:48 PM. Reason: Please use [code][/code] tags for greater readability.

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by raindeer
            Thank you for your help!

            I now use getline to read in every line one by one. But how do I evaluate the value of the first part of the line? For instance, if the first part is "Temperatur e", how do I put the second part of the line (the actual numerical value of the parameter) in a variable "B".

            Code:
            double B;
            char xxx[100];
            variable[99] = '\0'; 
            /* or just assign xxx[100] = ""; and it will clear the entire array.*/
            double value;
            scanf("%99s%lf", xxx, &yyy);
            
            if (xxx == "Temperature")
            	B = value;
            will not do anything

            Tom
            That is because xxx == "Temperatur e" is not doing as you are expecting it to do. What that statement says is ``Does xxx point to the same location as the constant string "Temperature",' ' which it doesn't. To compare c-strings, use the strcmp() function. It too doesn't do exactly what you expect at first, so read up on it prior to using it.


            Adrian

            Comment

            • bigturtle
              New Member
              • Apr 2007
              • 19

              #7
              The easy way to do it is to store just the input in the input file. Thus in your example your input file would contain just, say,

              1.0 1.0 1.0
              10
              298

              I'm assuming, perhaps wrongly, that this is a simple program, and that you are always reading the same items in the same order. The text 'boxlengthX', 'radius (nm)' etc are just documentation so that you can read the input file and make changes to the right values: those text items are not important to the program.

              To include the documentation in the input file, the easy way is to put it after the values, on the same line, thus:

              1.0 1.0 1.0 boxlengthX, Y, Z
              10 radius (nm)
              298 Temperature (K)

              Now you can read the values and ignore the remainder of each input line. The format of the text at the ends of the lines doesn't matter, since it's all comments that the program doesn't care about, and you don't need to worry about reading across it.

              This is a simple solution -- if your problem is more sophisticated and you are actually reading that text, putting items in various orders etc, my apologies.

              Comment

              • raindeer
                New Member
                • Mar 2007
                • 6

                #8
                Thank you for your advice!

                Bigturtle, the idea is indeed to have a rather big input file in the end, so I need to know which numbers represent which parameters.
                Any function of the program will scan the parameters in the input file and store the values that it needs in local variables.
                I have read the man files on strcmp (or strncmp) and this is what I have. It compiles but gives segment violation when running. I suppose something is wrong when comparing two strings of different lengths? However, playing around with that didn't solve anything. Also placing the values first and the documentation after it didn't solve it. Any ideas?

                Code:
                #include <stdio.h>
                #include <string.h>
                
                char str1[] = "Temperature";
                
                char *memal;
                int nbytes = 1000;
                
                
                main ()
                {
                while (getline(&memal, &nbytes, stdin) != -1)
                	{
                char variable[100];
                variable[99] = '\0'; 
                /* or just assign xxx[100] = ""; and it will clear the entire array.*/
                double value;
                scanf("%99s%lf", variable, &value);
                if (strncmp(str1, variable, 6) == 0)
                		{
                		printf("%.2lf\n", value);
                		}
                	}
                }

                Comment

                • AdrianH
                  Recognized Expert Top Contributor
                  • Feb 2007
                  • 1251

                  #9
                  Originally posted by raindeer
                  Thank you for your advice!

                  Bigturtle, the idea is indeed to have a rather big input file in the end, so I need to know which numbers represent which parameters.
                  Any function of the program will scan the parameters in the input file and store the values that it needs in local variables.
                  I have read the man files on strcmp (or strncmp) and this is what I have. It compiles but gives segment violation when running. I suppose something is wrong when comparing two strings of different lengths? However, playing around with that didn't solve anything. Also placing the values first and the documentation after it didn't solve it. Any ideas?

                  Code:
                  #include <stdio.h>
                  #include <string.h>
                  
                  char str1[] = "Temperature";
                  
                  char *memal;
                  int nbytes = 1000;
                  
                  
                  main ()
                  {
                  while (getline(&memal, &nbytes, stdin) != -1)
                  	{
                  char variable[100];
                  variable[99] = '\0'; 
                  /* or just assign xxx[100] = ""; and it will clear the entire array.*/
                  double value;
                  scanf("%99s%lf", variable, &value);
                  if (strncmp(str1, variable, 6) == 0)
                  		{
                  		printf("%.2lf\n", value);
                  		}
                  	}
                  }
                  What compiler are you using? Have you tried to step through your code with a debugger to determine where the problem is? Also, you are reading in the line already, you probably were wanting to use sscanf() instead of scanf().


                  Adrian

                  Comment

                  Working...