473,322 Members | 1,345 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,322 software developers and data experts.

file input/output

Hia,

I have to write a program in C to calculate the determinant of a matrix. I have mannaged to write a program which achieves for elements input by a user, however the program must take matrix elements from a file rather than a user.

The file is in the form,
3.0 2.0 -4.9
1.2 -4.3 6.1

I have been looking into ways of doing this and believe the best way to read the elements from the file is by using the fread function, however I do not understand how to use this for my purpose.

I wish to read one of the elements from the file, e.g. -4.9 and assign it to a floating point variable, so I want to say to the program " Program, open the file with file name xxxx.xxxx, go along to row 1, column 3 and assign the value at this location to a floating point variable A13" How can this be done if atall?

Any help would be much appreciated as I cant find a website that works through the problem at my level(basic).

Ps Has anyone got any suggestions for really good c programming books. I currently have Practical C programming by Steve Oualline, but i personally find it hard to follow.

Many Thanks

peter
Nov 7 '07 #1
2 1663
oler1s
671 Expert 512MB
Aside: I’m curious as to how you will take a determinant of a non square matrix. Clearly, your program needs to figure out how to deal unexpected inputs. If this is for an assignment, you might not care. In real life programming, you would.

I have been looking into ways of doing this and believe the best way to read the elements from the file is by using the fread function
It’s good that you looked around. But you’re wrong in this case. fread is great if you have a file and you just want to get the raw data (binary data). Here, you’re clearly trying to interpret the file as characters. Well, more precisely, you want to interpret them as numbers.

You could use fscanf repeatedly on the file. Or probably a bit more robust is to use fgets to store each line in a string. Once you have a string, you can attempt to convert the contents into doubles using strtod. This whole fgets and strtod method is well covered in online tutorials and forums, as it is a pretty common thing (i/o is obviously very common). Thankfully, you’re adept at using Google, so see if you can pick up something from online. If not, come back here and we’ll help you with examples.

The defacto book for C is by K&R. I didn’t anything else was acceptable, but I mostly focused on C++ anyway. If you’re serious about C (or C++), you’ll get K&R’s book anyway...
Nov 8 '07 #2
Aside: I’m curious as to how you will take a determinant of a non square matrix. Clearly, your program needs to figure out how to deal unexpected inputs. If this is for an assignment, you might not care. In real life programming, you would.

It’s good that you looked around. But you’re wrong in this case. fread is great if you have a file and you just want to get the raw data (binary data). Here, you’re clearly trying to interpret the file as characters. Well, more precisely, you want to interpret them as numbers.

You could use fscanf repeatedly on the file. Or probably a bit more robust is to use fgets to store each line in a string. Once you have a string, you can attempt to convert the contents into doubles using strtod. This whole fgets and strtod method is well covered in online tutorials and forums, as it is a pretty common thing (i/o is obviously very common). Thankfully, you’re adept at using Google, so see if you can pick up something from online. If not, come back here and we’ll help you with examples.

The defacto book for C is by K&R. I didn’t anything else was acceptable, but I mostly focused on C++ anyway. If you’re serious about C (or C++), you’ll get K&R’s book anyway...
Thanks for your help, I will have a look at these functions and check out how to use them. N.B. the matrix at the top of the page was an example to show the layout of the file I need to read from. The matrix I am dealing with is 3x3 square only which makes things much easier.
Nov 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: suresh | last post by:
Hello All, Iam writing a wsdl file using the Servicedescription class.The document I get is a well formed xml document.But some how when I try to add this wsdl file as a webrefernce to another...
1
by: moonriver | last post by:
In a xml file, can we make reference to another xml file so that all contents of the latter xml file will be included into the first xml file? Had better give me an example for details.
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
4
by: pank7 | last post by:
hi everyone, I have a program here to test the file IO(actually output) with buffer turned on and off. What I want to see is that there will be obvious differece in time. Here I have an input...
6
by: neo | last post by:
Hello everyone, I want to write a function that takes a simple text file as input and outputs a file such that the last line in the input file becomes the first line in the output file and the...
1
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
0
by: jebbyleezer | last post by:
Hello, I have source code that builds correctly, however, after the program terminates the output file produced is empty. Here is my source code: import java.io.*; import java.util.Scanner;...
0
by: Craftkiller | last post by:
=========Program compiles on both windows and linux=========== Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the...
4
by: rsaharia | last post by:
Hello All, I need help with this particular .pl file I picked up from http://www.veritools-usa.com/xnf2vhdl.htm What it's supposed to do is really convert an xnf file to a vhdl file. I need it for...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.