Help to a projekt

This is a discussion on Help to a projekt within the C Programming forums, part of the General Programming Boards category; My program is supposed to read from a file, and then put the data in my struct. Then I want ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Help to a projekt

    My program is supposed to read from a file, and then put the data in my struct. Then I want to use those data from the struct to calculate in different function and in the end, display it in a file.

    My code so far:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <stdbool.h>
    
    #define N 20
    #define A 0.0025
    #define Cth 22.5
    #define Rth 10.0
    #define v 15.0
    #define t_start 23.8
    #define T_a 23
    
    struct data
    {
    	double time;
    	double temp;
    	double current;
    };
    
    
    void inread(char str[],FILE *ovn_temp)
    {
    	fgets(str, sizeof(str),ovn_temp);
    }
    
    
    bool check(char str[])
    {
    	char str2[] = "-OL-";
    	char str3[] = "#";
    	if(strstr(str,str2)== NULL && strstr(str,str3)== NULL)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    
    void convert(char line[],struct data P)
    {
    	char *substr;
    	char *i_time,*i_temp,*i_current;
    
    	substr = strchr(line,' ');
    	strncpy(i_time, line,substr-line);
    	line = substr + 1;
    	P.time = atof(i_time);
    
    	substr = strchr(line,' ');
    	strncpy(i_temp, line,substr-line);
    	line = substr + 1;
    	P.temp = atof(i_temp);
    
    	substr = strchr(line,'\n');
    	strncpy(i_current, line,substr-line);
    	P.current = atof(i_current);
    }
    
    
    int read_line(char str[], int n)
    {
    	int ch, k = 0;
    
    	while(ch != '\n' && ch != EOF)
    	{
    		if(k < n)
    		{
    			str[k++] = ch;
    			ch = getchar();
    		}
    	}
    
    	str[k] = '\0';
    
    	return k;
    }
    
    double p_current(struct data P )
    {
    	double power;
    
    	power = v * P.current;
    
    	return power;
    }
    
    void t_ovn(struct data P, int k)
    {
    	double p_k, p_s;
    
    	p_k = (1/Rth) * ( t_ovn-T_a );
    
    	p_s = A * pow(5.67,10*(-8))*(pow(t_ovn,4) - pow(T_a,4) );
    
    	t_ovn = t_ovn+(k-1) + 1/Cth * (p_current * (k-1) - p_k*(k-1) - p_s*(k-1)) * (k - (k-1));
    }
    
    void print_result(struct data P)
    {
    	printf("%s \t %s \t %s \t %s", P.time, p_current,P.temp,t_ovn);
    }
    
    void average(struct data P)
    {
    	double average;
    
    	average = (t_ovn / (P.temp));
    }
    
    
    int main(void)
    {
    
    	FILE *ovn_temp,*udskrift;
    	ovn_temp = fopen("ovn_temp.txt","r");
    	udskrift = fopen("udskrift.txt","w");
    
    
    	int n = 5000,k;
    	double t_ovn;
    	char str[N],line[N];
    	struct data P;
    
    	while(!feof(ovn_temp))
    	{
    		inread(str,ovn_temp);
    		check(str);
    		convert(line, P);
    		print_result(P);
    		printf("%s %s %s",P.time, P.temp, P.current);
    	}
    
    	fclose(ovn_temp);
    	fclose(udskrift);
    
    	return 0;
    }

    I know their is a lots wrong with this. But I'm trying to take it from and end.

    First of all, why won't my main print anything to the screen?

    Thanks in advanced.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Arrays when passed to other functions, degrade to just pointers. So sizeof(array) is just the size of a pointer (maybe 4 bytes?). Way too small for your line of text.

    You'll need to either do the fgets() part in main(), or pass the sizeof(array), that you get from main(), into the inread() function.

    Also, feof is pretty much unreliable, depending on how the last bytes are set up in the file. Tends to repeat the last bytes twice before exiting properly.

    Code:
    while((fgets(buffer, sizeof(buffer), yourFilePointer)) != NULL) {
       //your other code here
    }
    Works well. Always a good idea to test your file pointers != NULL so you know the files have been opened OK.

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21