I wrote a c function to extract useful information of a txt file into another txt file using fprintf and fscanf, but i can't get it work, i don't know what's wrong, Please Help me.
Here is the format of the input txt file:
Average Values for the last 100 out of 100 Dynamics Steps
Simulation Time 0.5000 Picosecond
Total Energy -129266.4088 Kcal/mole (+/- 519.6292)
Potential Energy -130953.3057 Kcal/mole (+/- 318.7607)
What i need is the first string ,such as "Total", and the corresponding floating point data, such as "-129266.4088".
Actually, there are thousands of such items, and i need the c program to extract the information.
Here is my c program,
for( int index = 0 ; index < 6 ; ++index ) {
if( strcmp( str , str_arr[ index ] ) {
char * tmp_str; // tmp_str presents just to receive the word "Energy" and jump over it
double data;
fscanf( rfp, "%s%f",tmp_str, &data );
#ifdef DEBUG
prinf( "%s %f\n", str, data );
#endif
it compiles well but terminates unexpectedly every time i run it, it can also print the first two strings "Total","Energy" and a double value "0.000000", then corrupts. WHAT'S WRONG? Please help me! Thanks.
you are not creating a string; you are creating a pointer to a char.
To create a string, you must dynamically allocate some memory and assign the address of that memory to your pointer.
In C, the function used for this is malloc. To get malloc, include the stdlib.h header file. Malloc takes a single argument: the amount of memory to allocate. It returns a pointer to a contiguous chunk of memory of the specified size. If the allocation fails, malloc returns NULL, to indicate that an error occurred.
Here's an example. Let's use malloc to create a string that can hold up to 128 characters (including the terminating null):
//header file that contains malloc
#include <stdlib.h>
//header file that contains printf
#include <stdio.h>
int main(){
//pointer to the first character of our string
char *str;
//Allocate memory. The sizeof() operator tells us how much much memory the specified datatype needs.
//(It is important to use sizeof, as the amount of memory datatypes need can be different on different platforms.)
//The stuff within the parenthesis right before malloc is a type cast, which converts the void pointer
//returned by malloc to a char pointer. A type cast is not strictly necessary here; however, it is a good habit to use it.
str = (char *) malloc(128 * sizeof(char));
//Now we check to make sure that the allocation was successful. YOU MUST ALWAYS DO THIS.
//NEVER ATTEMPT TO USE MALLOC WITHOUT CHECKING ITS RESULT.
//If malloc failed, str will be set to NULL.
if(str == NULL){
//Malloc failed. Print an error and exit.
printf("Error: malloc failed\n");
return -1;
}
//At this point the program would do something useful...
return 0;
}
Please keep in mind that the string's size is fixed, and it you try to put too much data into it your program will probably crash. To prevent this, keep track of the number of characters that are loaded from the file, and stop loading characters if you run out of space in the string.
After some modification, it works well, but it still has one mysterious problem.
If I add the following lines at the beginning of the input file, the well-performed program corrupts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
##############################################################################
##############################################################################
## ##
## TINKER --- Software Tools for Molecular Design ##
## ##
## Version 5.0 October 2007 ##
## ##
## Copyright (c) Jay William Ponder 1990-2007 ##
## All Rights Reserved ##
## ##
##############################################################################
##############################################################################
Notice there is a newline at the very beginning.
I am pretty sure that it is this part collapse the whole thing. But i don't understand why, could anybody explain to me? Thanks very much.
You might want to read line by line instead of %s by %s, and then process the character line in your program. If the line contains only whitespace, or if it begins with whitespace followed by a hash symbol '#', then you can ignore it and continue on to the next line.
Well, I know the problem.
In the modified version, i define the str a array of char of size 20, but the "####..." 's size is greater than 20, so the program collasped. After second modification, the program works well.
Thanks everyone helped me. I won't make such mistakes anymore. Thank you again.