Hello Friends,,
I m really a new bee to C programms , please help me with a code..
I found some theads here similar to this but Not able to solve what exactly I want..
suppose I ve txt file as below.
abc.txt
12 23
10 11
131 159
12.2 13.8
Then I want to create a file which will read the file and store to
double pointer variable , as below
double *a;
double *b
a = 12 // the pointer a should contain the real value 12
b = 23 //the pointer b should contain the real value 23
Then I need to send these pointer value to another function.
Please help need urgently..
user_prady:
Hello Friends,,
I m really a new bee to C programms , please help me with a code..
I found some theads here similar to this but Not able to solve what exactly I want..
suppose I ve txt file as below.
abc.txt
12 23
10 11
131 159
12.2 13.8
Then I want to create a file which will read the file and store to
double pointer variable , as below
double *a;
double *b
a = 12 // the pointer a should contain the real value 12
b = 23 //the pointer b should contain the real value 23
Then I need to send these pointer value to another function.
Please help need urgently..
I am not sure if I understood you completely, but are you sure you need those double values to be stored in pointers to double values (not in doubles they point to)? Anyway, you can read your file line by line with fgets and scan each line with sscanf. Like this way:
#include <stdio.h>
#include <stdlib.h>
void process_doubles(double* a,double* b)
{
*a+=0.1;
*b+=0.1;
}
void parse_file(const char* szFn)
{
FILE* fh;
char szBuf[64];
double a,b;
fh=fopen(szFn,"r");
while(!feof(fh))
{
fgets(szBuf,sizeof(szBuf),fh);
if(sscanf(szBuf,"%lf %lf\n",&a,&b)==2)
{
process_doubles(&a,&b);
printf("%f %f\n",a,b);
}
}
}
int main(int argc,char** argv)
{
if(argc<2) { fprintf(stderr, "Usage: %s <file name>\n",argv[0]); exit(0); }
parse_file(argv[1]);
exit(0);
}
I omitted some really important checkings like if the file with particular file name actually exists, but in principal this should work.
user_prady, is this a homework question? Please note that rule #6 prohibits homework questions from being posted.
neutrino4:
I am not sure if I understood you completely, but are you sure you need those double values to be stored in pointers to double values (not in doubles they point to)? Anyway, you can read your file line by line with fgets and scan each line with sscanf. Like this way:
#include <stdio.h>
#include <stdlib.h>
void process_doubles(double* a,double* b)
{
*a+=0.1;
*b+=0.1;
}
void parse_file(const char* szFn)
{
FILE* fh;
char szBuf[64];
double a,b;
fh=fopen(szFn,"r");
while(!feof(fh))
{
fgets(szBuf,sizeof(szBuf),fh);
if(sscanf(szBuf,"%lf %lf\n",&a,&b)==2)
{
process_doubles(&a,&b);
printf("%f %f\n",a,b);
}
}
}
int main(int argc,char** argv)
{
if(argc<2) { fprintf(stderr, "Usage: %s <file name>\n",argv[0]); exit(0); }
parse_file(argv[1]);
exit(0);
}
I omitted some really important checkings like if the file with particular file name actually exists, but in principal this should work.
Thank you Neutrino .. Really it helps me a lot..
GOD Bless You..
Hi Neutrino !
I got you
File should be closed at the end of the function parse_file
fclose(fh);
Dear Friends,
I am really in a great problem.
My problem is as follows
If I will send a counter to a function in c like read_line(counter);
Then it will read that line only which counter is pointing ,
Suppose I ve txt file like below
abc.txt
12.1
13.2
14.4
Then If I'll call the function as read_line(2);
It should read the 2nd line only and then put that value to a double pointer..
I mean one function call should read only the specified row,, and store it to a double pointer..
I need help badly..
No, its not a home work at all .
I am implementing C with my verilog code(HDL).
FYI This is going to be a chip of future for your mobile ..
Yet I am very confuse how to do this ..
With Regards,
user_pradyu
user_prady:
Dear Friends,
I am really in a great problem.
My problem is as follows
If I will send a counter to a function in c like read_line(counter);
Then it will read that line only which counter is pointing ,
Suppose I ve txt file like below
abc.txt
12.1
13.2
14.4
Then If I'll call the function as read_line(2);
It should read the 2nd line only and then put that value to a double pointer..
I mean one function call should read only the specified row,, and store it to a double pointer..
I need help badly..
One way you could do this is by moving the sscanf part of the code into the read_line() function. This way you just need to increment the variable by 1 after every call to fgets. After calling fgets you can invoke read_line() and pass the counter as an argument to it and inside read_line() the double floating number will be stored at the address specified by the double pointer using sscanf.