473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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
May 8 '07 #1
8 1634
weaknessforcats
9,208 Expert Mod 8TB
scanf() should work.

If a file item looks like:

boxlengthX 1.0

then
Expand|Select|Wrap|Line Numbers
  1. scanf("%s%f", xxx,yyy); 
  2.  
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.
May 8 '07 #2
AdrianH
1,251 Expert 1GB
scanf() should work.

If a file item looks like:

boxlengthX 1.0

then
Expand|Select|Wrap|Line Numbers
  1. scanf("%s%f", xxx,yyy); 
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. char xxx[100];
  2. double yyy;
  3. scanf("%s%lf", xxx, &yyy);
To safely do it you would ensure you are not overruning the buffer:
Expand|Select|Wrap|Line Numbers
  1. char xxx[100];
  2. xxx[99] = '\0'; // or just assign xxx[100] = ""; and it will clear the entire array.
  3. double yyy;
  4. scanf("%99s%lf", xxx, &yyy);
To ensure you have read in all of the data:
Expand|Select|Wrap|Line Numbers
  1. char xxx[100];
  2. xxx[99] = '\0'; // or just assign xxx[100] = ""; and it will clear the entire array.
  3. double yyy;
  4. if(2 == scanf("%99s%lf", xxx, &yyy)) {
  5.   // read in both string and double
  6. }
  7. else {
  8.   // not read in both string and double
  9. }
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:
Expand|Select|Wrap|Line Numbers
  1. int parsedTo = 0;
  2. char xxx[100];
  3. xxx[99] = '\0'; // or just assign xxx[100] = ""; and it will clear the entire array.
  4. double yyy;
  5. scanf("%99s%lf%n", xxx, &yyy, &parsedTo)
  6. if (parsedTo != 0) {
  7.   // read in both string and double
  8. }
  9. else {
  10.   // not read in both string and double
  11. }
Hope this helps.


Adrian
May 8 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. scanf("%s%f", xxx,yyy); 
  2.  
Sorry for the confusion here. I intended xxx and yyy to mean "appropriate arguments". I'll be more careful in the future.
May 8 '07 #4
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 "Temperature", how do I put the second part of the line (the actual numerical value of the parameter) in a variable "B".

Expand|Select|Wrap|Line Numbers
  1. double B;
  2. char xxx[100];
  3. variable[99] = '\0'; 
  4. /* or just assign xxx[100] = ""; and it will clear the entire array.*/
  5. double value;
  6. scanf("%99s%lf", xxx, &yyy);
  7.  
  8. if (xxx == "Temperature")
  9.     B = value;
  10.  
will not do anything

Tom
May 9 '07 #5
AdrianH
1,251 Expert 1GB
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 "Temperature", how do I put the second part of the line (the actual numerical value of the parameter) in a variable "B".

Expand|Select|Wrap|Line Numbers
  1. double B;
  2. char xxx[100];
  3. variable[99] = '\0'; 
  4. /* or just assign xxx[100] = ""; and it will clear the entire array.*/
  5. double value;
  6. scanf("%99s%lf", xxx, &yyy);
  7.  
  8. if (xxx == "Temperature")
  9.     B = value;
  10.  
will not do anything

Tom
That is because xxx == "Temperature" 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
May 9 '07 #6
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.
May 9 '07 #7
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?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char str1[] = "Temperature";
  5.  
  6. char *memal;
  7. int nbytes = 1000;
  8.  
  9.  
  10. main ()
  11. {
  12. while (getline(&memal, &nbytes, stdin) != -1)
  13.     {
  14. char variable[100];
  15. variable[99] = '\0'; 
  16. /* or just assign xxx[100] = ""; and it will clear the entire array.*/
  17. double value;
  18. scanf("%99s%lf", variable, &value);
  19. if (strncmp(str1, variable, 6) == 0)
  20.         {
  21.         printf("%.2lf\n", value);
  22.         }
  23.     }
  24. }
  25.  
May 10 '07 #8
AdrianH
1,251 Expert 1GB
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?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. char str1[] = "Temperature";
  5.  
  6. char *memal;
  7. int nbytes = 1000;
  8.  
  9.  
  10. main ()
  11. {
  12. while (getline(&memal, &nbytes, stdin) != -1)
  13.     {
  14. char variable[100];
  15. variable[99] = '\0'; 
  16. /* or just assign xxx[100] = ""; and it will clear the entire array.*/
  17. double value;
  18. scanf("%99s%lf", variable, &value);
  19. if (strncmp(str1, variable, 6) == 0)
  20.         {
  21.         printf("%.2lf\n", value);
  22.         }
  23.     }
  24. }
  25.  
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
May 10 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
2
by: xAvailx | last post by:
I have a requirement that requires detection of rows deleted/updated by other processes. My business objects call stored procedures to create, read, update, delete data in a SQL Server 2000 data...
0
by: Matthew Heironimus | last post by:
According to the XML 1.0 (Third Edition) W3C Recommendation (http://www.w3.org/TR/2004/REC-xml-20040204/#sec-line-ends) all #xD, #xA, and #xD#xA character combinations should be converted to a...
1
by: Matthew Heironimus | last post by:
According to the XML 1.0 (Third Edition) W3C Recommendation (http://www.w3.org/TR/2004/REC-xml-20040204/#sec-line-ends) all #xD, #xA, and #xD#xA character combinations should be converted to a single...
7
by: MM | last post by:
Hi there, How can I change my code (below) so that I use an "input argument" to specify the file name of the input file? For example, if I compile the code and that the application then gets the...
5
by: Chathu | last post by:
Hello everyone........... I have a problem on retriving a content of a binary file I wrote into. My program user structures, dynamic allocation of memory and files. I take the infomation into a...
4
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module...
4
by: Tad Marshall | last post by:
Hi, I'm having limited luck getting an ApplicationException to work right in my code. This is VB.NET, VS 2003, Windows XP SP2, .NET Framework 1.1. I thought it would be convenient to take...
1
by: sudhivns | last post by:
Looking for Zip/Unzip handling code. Unzip feature should extract the files including the absolute path. Eg: Input data.zip contains 1. folder IMP 2. some *.txt files. On Unzip, i choose...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.