473,399 Members | 3,106 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,399 software developers and data experts.

read float value from file

Dear friends,
In the following code, I can't read the float value from
file(temp.txt). Run-time error is coming. I am using Visual C++.

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int main()
{

float a;
char temp[50],path[50];
FILE *in;

strcpy(path,"D:\\user\\smp\\rajesh\\temp\\");
strcpy(temp,path);
if((in=fopen(strcat(temp,"temp.txt"),"r")) == NULL)
{
printf("Error : Opening file in.txt!!!!!\n\n\n");
return 0;
}

fscanf(in,"%f",&a);
return 0;
}

Please guide me.

-Mahesh

Jul 22 '05 #1
1 5550
Mahesh wrote:
Dear friends,
In the following code, I can't read the float value from
file(temp.txt). Run-time error is coming. I am using Visual C++.

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
int main()
{

float a;
char temp[50],path[50];
FILE *in;

strcpy(path,"D:\\user\\smp\\rajesh\\temp\\");
strcpy(temp,path); I really can't figure out why you need the above line.
Try this:
const unsigned int MAX_FILE_NAME = 50;
const char * const Directory_Name =
"D:\\user\\smp\\rajesh\\temp\\";
const char * const File_Name = "temp.txt";
char complete_filename[MAX_FILE_NAME];

assert(MAX_FILE_NAME > (sizeof(Directory_Name)
+ sizeof(File_Name));

strcpy(complete_filename, Directory_Name);
// strcat(complete_filename, File_Name);
strcpy(complete_filename + sizeof(Directory_Name) - 1,
File_Name); // this one is a little faster than
// strcat.
in = fopen(complete_filename, "r");
if (!in)
{
printf("Error opening: \"%s\"\n", complete_line);
return EXIT_FAILURE;
}

if((in=fopen(strcat(temp,"temp.txt"),"r")) == NULL)
{
printf("Error : Opening file in.txt!!!!!\n\n\n");
return 0;
}

fscanf(in,"%f",&a);
return 0;
}

Please guide me.

-Mahesh


For more details, post your data file.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: nooy66 | last post by:
I need to read value type double form my binary file. When i use Hex Workshop software read my binary file. i have data 8 bytes : hex = 3131 3131 3131 3140 string = 1111111@ Float =...
3
by: Robb Gilmore | last post by:
Hello, We have a C#.NET app which is calling a Java webservice. We use the wsdl file exportted from the java webservice to create our web-reference in Visual Studio. We are able to create the...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
1
by: Jose Reckoner | last post by:
I'm running python 2.3 on Windows XP. Anyone have a quick small script to convert .DT1 and .DEM data to ASCII or some other format? I don't need a viewer. Thanks!
3
by: phwashington | last post by:
I am new to C++ and have a data file I want to read, which was stored in binary. I have looked at the data with a hex editor and it appears to be correct. Whenever I try to read it though as an...
2
by: RyanS09 | last post by:
Hi- I have read many posts with specific applications of reading in text files into arrays, however I have not been able to successfully modify any for my application. I want to take a text file...
4
by: jx2 | last post by:
hi everyone i'm trying to read data from CSV file file is about 1MB (or bigger) i figured out how to read it quickly from the disc but it takes 3000 milliseconds to change it into arrays so my...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.