c file to extract real value from a txt file


 
Thread Tools Search this Thread
Top Forums Programming c file to extract real value from a txt file
# 1  
Old 01-31-2008
c file to extract real value from a txt file

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
Code:
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


[code ]
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
[/code]

Then I need to send these pointer value to another function.

Please help need urgently..
# 2  
Old 02-01-2008
Quote:
Originally Posted by 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
Code:
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


[code ]
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
[/code]

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:
Code:
#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.
# 3  
Old 02-01-2008
user_prady, is this a homework question? Please note that rule #6 prohibits homework questions from being posted.
# 4  
Old 02-04-2008
Quote:
Originally Posted by 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:
Code:
#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..
# 5  
Old 02-04-2008
Hi Neutrino !

I got you Smilie

File should be closed at the end of the function parse_file

Code:
fclose(fh);

Smilie
# 6  
Old 02-04-2008
Quote:
Originally Posted by matrixmadhan
Hi Neutrino !

I got you Smilie

File should be closed at the end of the function parse_file

Code:
fclose(fh);

Smilie
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..
# 7  
Old 02-04-2008
Quote:
Originally Posted by blowtorch
user_prady, is this a homework question? Please note that rule #6 prohibits homework questions from being posted.
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To extract values after the maximum value in a txt file

Hello, I'm new to scripting and I need to write a bash script. Here is example of file on which I'm working: 0.3092381 0.3262799 0.3425480 0.3578379 0.3719490 0.3846908 0.3958855 0.4053738 0.4130160 0.4186991 0.4223357 ... (1 Reply)
Discussion started by: jeo_fb
1 Replies

2. Shell Programming and Scripting

Extract information from txt file

Hello! I need help :) I have a file like this: AA BC FG RF TT GH DD FF HH (a few number of rows and three columns) and I want to put the letters of each column in a variable step by step in order to give them as input in another script. So I would like to obtain: for the 1° loop:... (11 Replies)
Discussion started by: edekP
11 Replies

3. Shell Programming and Scripting

Script extract text from txt file with grep

All, I require a script that grabs some text from the gitHub API and will grep (or other function) for a string a characters that starts with (") quotes followed by two letters, may contain a pipe |, and ending with ) . What i have so far is below but it's not returning anything. ... (4 Replies)
Discussion started by: ChocoTaco
4 Replies

4. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

5. Shell Programming and Scripting

Need to append the date | abcddate.txt to the first line of my txt file

I want to add/append the info in the following format to my.txt file. 20130702|abcd20130702.txt FN|SN|DOB I tried the below script but it throws me some exceptions. <#!/bin/sh dt = date '+%y%m%d'members; echo $dt+|+members+$dt; /usr/bin/awk -f BEGIN { FS="|"; OFS="|"; } { print... (6 Replies)
Discussion started by: harik1982
6 Replies

6. Shell Programming and Scripting

Command to extract all columns except the last few from a txt file

hello, i have publicly available txt file with little less than 300000 rows. i want to extract from column 1 to column 218 and save it in another text file. i use the cut command but the file is saved with multiple rows from the source file onto a single row in the destination. basically it is... (6 Replies)
Discussion started by: madrazzii
6 Replies

7. Shell Programming and Scripting

awk append fileA.txt to growing file B.txt

This is appending a column. My question is fairly simple. I have a program generating data in a form like so: 1 20 2 22 3 23 4 12 5 43 For ever iteration I'm generating this data. I have the basic idea with cut -f 2 fileA.txt | paste -d >> FileB.txt ???? I want FileB.txt to grow, and... (4 Replies)
Discussion started by: theawknewbie
4 Replies

8. UNIX for Dummies Questions & Answers

Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!! The .txt file looks essentially like this... (5 Replies)
Discussion started by: eggali
5 Replies

9. Shell Programming and Scripting

Extract from txt file

I have data as follow in the txt file. I want to skip line starting with '#' sign. #command program abc defmt exp refmt ... ... I want to store abc exp .... in a array. I want to store defmt refmt in a array I need command to read each line in the file. I need... (6 Replies)
Discussion started by: ekb
6 Replies

10. UNIX for Dummies Questions & Answers

Binary txt file received when i use uuencode to send txt file as attachment

Hi, I have already read a lot of posts on sending attachments in unix...but none of them were of help for my problem...so here goes.. i wanna attach a text file and send to a mail id..used the following code : uuencode "$File1" "$File1" ;|mail -s "$Mail_sub" abc@abc.com it works... (2 Replies)
Discussion started by: ash22
2 Replies
Login or Register to Ask a Question