473,405 Members | 2,176 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,405 software developers and data experts.

fscanf and pointers

Hey,

I need to adapt an existing c program and have to read out some values and then put them back in. Reading out the values works fine, but reading them in fails. Does anyone know how I can do this, I think it has something to do with structures, but I don't know anything about them. Here is wat I have:

Expand|Select|Wrap|Line Numbers
  1. ssprofileout=fopen("ssprofile.dat","a"); /* writes a file named ssprofile.dat with i, x[i] and h[i] */
  2. for (i=istart; i<iend; i++) {
  3. if ((d.e[i] > 0. || d.e[i+50] > 0.) || (d.e[i] > 0. || d.e[i-50] > 0.))
  4. fprinf(ssprofileout,"%i %g %g\n", i,d.x[i],d.h[i]); }
  5. fclose(ssprofileout);
this is the part that works, it gives three arrays. Now I want to put these arrays back into the program by doing this

Expand|Select|Wrap|Line Numbers
  1. case SSPROFILE:
  2. ssprofilein=fopen("ssprofile.dat","r");
  3.  
  4. for(i=0; i<(iend-istart); i++) {
  5. fscanf(ssprofilein,"%i %g %g",i,&d.x[i],&d.h[i]);
  6. d->h[i]=d.h[i];
  7. d->x[i]=d.x[i];
  8. fclose(ssprofilein);
  9.  
This obviously doens't work with pointers.. how could I change this in order to makes this work?

Beuwer
Jun 13 '11 #1
5 2305
weaknessforcats
9,208 Expert Mod 8TB
It appears this has nothing o do with structures or pointers. What it does have to do wih is that any data written back to a file as an update must be written in preciselythe eaxct locaion it cam from. That means a) you need to know the file layout and you need to put your update at a precise locaion based on that layout.

so if you know an integer was located 36 bytes from the start of the file and occupies 4 bytes, then yur update must be written at that exact location.

Research seek() and tell(). Examples showing use of seek() and tell() frequenty include one on how to update a file.
Jun 13 '11 #2
The program is really big and complex, so I don't know how to find the exact location based on the lay-out, but the errors the program gives are:

error: request for member âxâ in something not a structure or union
error: request for member âhâ in something not a structure or union

in the second c part of my question lines 5,6 and 7..

I don't know if this may give you some more information, because I still don't know what to do
Jun 14 '11 #3
Banfa
9,065 Expert Mod 8TB
Take line 6

Expand|Select|Wrap|Line Numbers
  1. d->h[i]=d.h[i];
d can not possibly be a structure and a pointer to a structure (actually not quite true in C++ for example iterators can act as both), which is it and what is the purpose of this statement.
Jun 14 '11 #4
d.h[i] is the output, and the program makes a new array which is calles d->h[i], so I wanted to assign the value of d.h[i] to d->h[i]
Jun 14 '11 #5
weaknessforcats
9,208 Expert Mod 8TB
Are you able to post the declaration for d? When you see d.something, then d is a variable of struct or class whereas d->something means d is an address. Usually two things cannot have the same name unless a)it's C++ and b)there is an overload of the -> operator.

I need to see a bit more.
Jun 14 '11 #6

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

Similar topics

4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
5
by: gb | last post by:
Hi, I have a small program which opens a file (Whose content is a mac address in the form xx:xx:xx:xx:xx:xx) and uses fscanf() to get individual bytes into an array which is defined as "unsigned...
6
by: joelperr | last post by:
Hello, I am attempting to separate a two dimensional array into two one-dimensional arrays through a function. The goal of this is that, from the rest of the program, a data file consisting of...
7
by: Joah Senegal | last post by:
Hello all, I;m trying to connect a set of linkedlists to a array of pointer. So the array contains pointers to the linkedlists... but how do I make a array of pointer?
9
by: quyvle | last post by:
I can't seem to get this function to work correctly. I'm wondering if anyone could help me out with this. So I'm using the fscanf function to read the input stream and store each string in the...
6
by: haloman | last post by:
Why does: char *myString = (char *) malloc(WORDLIMIT*sizeof(char)); fscanf(openFile, "%s", myString) != EOF; work yet char *myString = (char *) malloc(WORDLIMIT*sizeof(char));...
59
by: David Mathog | last post by:
Apologies if this is in the FAQ. I looked, but didn't find it. In a particular program the input read from a file is supposed to be: + 100 200 name1 - 101 201 name2 It is parsed by reading...
1
by: Bizancio | last post by:
Im doing a program in C, using visual studio and i have to make a list of players (struct). I have a pointer that marks FIRST and LAST element of the list. The problem is that when i try to pass the...
1
by: Matrixinline | last post by:
Hi All, File Text.txt Contains following text as : "C:\program file\application data\details\app" "D:\Program File" I tried to read that data as fscanf(oFp, "%s %s", sCopyDirectory,...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.