C :: Append Data To Array Of Structures In Memory Then Put In File When Program Ends
Dec 4, 2013
I have a program that stores health information the user inputs, one person at a time. The program works perfectly with the exception of storing the data...I need to open a file and read what health data it has in it already, if any, but store the new changes, and appended data to the array of structures, to the data in memory. All of the information is only saved back in the file once the program terminates. I'm not sure how to go about doing this, so I am also not sure what to put in the function for "Save and Exit" that the user can choose in order to exit the program.
Code:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
struct strdatabase
{
char first[20];
char last[20];
char gender[2];
[Code]...
I tried playing around with this bit of code, but I'm not sure if I'm even on the right track.
Code:
FILE *fp;
fp = fopen ("students.txt","rb"); fseek(fp, 0, SEEK_END);
long pos = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *bytes = malloc(pos);
fread(bytes, pos, 1, fp);
fclose(fp);
View 9 Replies
ADVERTISEMENT
Apr 16, 2014
I am a very fresh c programme user, and need to fscanf data to arrays. Somehow it just doesn't gets recorded, wonder what is wrong with my code?
When I printf the array it always ends up with random numbers. Below is my programme which is just designed to read a doc and print it back out, as well as the text doc it is designed to read.
View 3 Replies
View Related
Nov 2, 2014
I'm reading the following file:
Osgood,Marcus 298542123 CHM FR mosgood@whatever.edu
Cronk,Melissa 873489021 BIO SR mcronk@whatever.edu
Pry,Seth 349908431 MTH SO spry@whatever.edu
Langlais,Susan 783323545 ME SR slanglais@whatever.edu
Davis,Nicole 987543345 PHY FR ndavis@whatever.edu
It's supposed to split it up into name, ID number, major, year, and email. The file reads it without any errors, and assigns name to the first part of the structure. However, ID gets assigned the ID, major, year, and email. Then Major gets assigned major, year, and email. Year gets assigned year and email, while email just gets assigned email. I don't know if it has something to do with the loop. For example, this is what I get what I print just the name and the ID.
Cronk,Melissa 873489021BIOSRmcronk@whatever.edu
Pry,Seth 349908431MTHSOspry@whatever.edu
Langlais,Susan 783323545ME
Davis,Nicole 987543345 PHYFRndavis@whatever.edu
Anyway. This is my function code for reading the array. I have it printing the ID number just to see if I can catch the errors earlier:
Code:
#include <stdio.h>
#include <string.h>
#include "functions.h"
void read_data(char filename[], studentinfo_t s[], int *size) {
FILE *infilep;
int countstudents = 0;
infilep = fopen("student_data", "r");
[Code].....
the name is reading correctly, but not anything else!
View 1 Replies
View Related
May 13, 2012
I am trying to read from a data file that has input as :
12.0, 11, 123
14.0, 12.1, 3
And I want the program to read the data from the file and then make it into an array of structures and then print that array afterwards.
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_INPUT 1000
int n =0;
int i = 0;
typedef struct {
double x[MAX_INPUT];
[Code] .....
The program when run gives the following output:
Ishtiaque-Mohammed-Khans-MacBook-Pro:Comp20005 IshtiaqueMKhan$ gcc -Wall -ansi -o ProjectB ProjectB.c
ProjectB.c: In function "main":
ProjectB.c:59: error: incompatible type for argument 1 of "print_array"
View 1 Replies
View Related
Oct 22, 2014
I was recently introduced to the fstream header file. I want to know is their a easy way to print an output with append data in a .txt file.
And secondly I am also having an error with my header file. It says error: cannot open source file "fstring" when I hover my mouse over "#Include<fstring>".
View 7 Replies
View Related
Sep 26, 2012
I'm trying to append data to a CFile using FILE* But when execute the application, it always give error saying "No such file or directory". I can actually see the file created but it just keep giving error "No such file or directory".
Is the file being lock or the file just created so it can not be find by fstream(FILE*)? or the file mode is wrong?
Below is the code:
[QUOTE]
void SaveDocument(CString strFile) {
CFile file;
if( !file.Open(strFile, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone)) {
CArchive ar(&file, CArchive::store);
// save now.
MyClass.Serialise(ar);
[Code]...
Attached is the printscreen of the FILE* pointer. the pointer is evaluated as bad pointer. Why the FILE* pointer not able point to the file being created?
View 7 Replies
View Related
Jul 27, 2014
I have to allocate memory for an array of structures, and my structure looks as following:
Code:
typedef struct {
char *name;
Uint *start_pos;
Uint len;
}
example_struct;
And now I want to allocate memory, for a variable number (so an array) of example_struct, so I first do:
Code:
example struct *all_struct;
int total_num = 3;
//will be set somehow, but for the example I set it on 3 all_struct = malloc (sizeof(example_struct) * total_num);
And now, as far as I now, I will have to allocate for each field of the structure memory, in order to be able to use it later. But I have problem at this point, a problem of understanding:
- I just allocated memory for 3 structures, but don't I have to allocate then memory for each structure separately, or can I just now allocate the fields like this:
Code: all_struct[0].name = malloc.....
But if yes, why the hell this works...
View 10 Replies
View Related
Mar 16, 2013
I am trying to understand how to append some code to a text file. I have run a simple program like the one below. Basically it opens a text file and then it appends the string aaaaaaaaaa
Code:
#include <stdio.h>
#include <io.h>
#include <stdlib.h>
char s[10] = {"
aaaaaaaaaa"};
[Code] .....
Now that I know how to append the above string, I now would like to extend the experiment to append the following snipped of code in my test.txt file:
Code:
<Placemark>
<name>12:01</name>
<Snippet maxLines="0"></Snippet>
<description> </description>
[Code] ....
How do I go about doing this? The problem I am experiencing is that all the / and " characters in the above snippet of code seems to get the C compiler confused. One thought I had was to create a string like the one below but as I mentioned, the number 0 in the code is surrpunded by " " and this confuses the compiler:
Code:
char s[250] = {"<Placemark>
<name>12:01</name>
<Snippet maxLines="0"></Snippet>
[Code] ....
View 3 Replies
View Related
May 16, 2012
Trying to append a comma to a string. Getting "Segmentation Error" on Solaris when the function is entered the second time.
Code:
// Appends a comma to the given string
void appendComma(char* instring) {
if (instring == NULL) {
instring = realloc(NULL, strlen(","));
strcpy(instring,",");
[Code] .....
View 14 Replies
View Related
Dec 11, 2013
I am writing a employee payroll program using structures. My program is running but its only showing some of the data.
HERES MY CODE
Code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;
const int SZ = 20; // size of arrays to hold scores
struct payrollStruct {
[Code] ....
And it doesn't show anything from txt file
Code:
40.0 10.00 A1234 Jane Adams
50.0 10.00 L8765 Mary Lincoln
25.5 10.85 W7654 Martha Washington
52.0 15.75 A9876 John Adams
45.0 25.00 W1235 George Washington
[Code] .....
View 14 Replies
View Related
Jul 16, 2013
How can I play sound when the program ends? I am using Windows 7.
View 15 Replies
View Related
Apr 8, 2014
I am trying to read the information from a .bmp file into my program while dynamically allocating memory for all of it. So I defined the structures for the headers and for a pixel, and then I have to read each pixel into a multi-dimensional array of pixels. I am completely new to structures, and definitely new to pointers combined with structures. I will have to use a filter with the information later, but right now I just want to read everything in correctly. The array of pixels will be two-dimensional -- the width by the height of the image, I guess.
Here's my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *inFile, *outFile;
typedef struct
[Code] ....
View 4 Replies
View Related
Nov 26, 2014
// my size is not decreasing when I erase a record.
//dvr_hwch.cpp
#include <iostream>
#include <iomanip>
#include <cassert>
using namespace std;
#include "hash_chn.h"
void print_menu( );
[Code] .....
View 1 Replies
View Related
Jun 18, 2013
I am doing an exercise which has to do with International country codes.The user must give a code and the programm will display the corresponding country.
Code:
#include <stdio.h>
#define COUNTRY_COUNT
((int) (sizeof(country_codes) / sizeof(country_codes[0])))
[code]....
View 14 Replies
View Related
Mar 21, 2014
I have an assignment that wants me to better understand how certain calls like malloc() and free() work. I'm supposed to have one function that allocates a pool of memory for the program to use (this is the only place where I'm allowed to use the malloc function. Anyplace else is off limits, except when allocating space for data structures.) Another function needs to be defined that allocates memory from this large pool of memory that we have already allocated. A third function needs to free a block of memory when a call from function main is made and a fourth does something else (haven't gotten that far yet.) I'm not sure if it's ok or not, but I'll attach the pdf form that describes all the requirements. If that's not ok, I'll delete it.)
Anyways, when thinking of data structures to use for this particular problem, one doesn't jump out at me as being THE data structure that would be most useful. How I would imagine using it is to store the addresses currently being used as well as the block length of those addresses (so basically, each node of the linkedlist would have a pointer that points to an allocated address and another field that told the size of the allocated block of memory.) How to communicate to the large pool of memory that a certain block of memory has been allocated and shouldn't be used until a free is made on that specific block of memory.
Attached File(s)
pa3.pdf (61.07K)
View 2 Replies
View Related
Mar 26, 2013
I'm having issues trying to input data from a .txt file into my array program. The whole program takes the name, # of goals and assists, adds them together to get points, and then puts in a given + or - rating. The program works except for the data input.
Also, this is an alphabetical "Unsorted" list, but is there a way to order the list based on point value and be able to display that as well as a separate "Ordered" list?
#include <iostream>
#include <iomanip>
#include <fstream>
#define NUM_PLAY 30
using namespace std;
int buildArrays(int[], int[], int[]);
void printArrays(string[], int[], int[], int[], int);
[Code]...
And this is the .txt file. Order is Players Name, Goals, Assists, Plus/Minus rating. The points are added when points = goals + assists (Should be in the main coding above).
Bryan_Bickell 2 5 +2
Brandon_Bollig 0 0 0
Dave_Bolland 4 2 -1
Sheldon_Brookbank 0 0 -1
Daniel_Carcillo 0 1 +3
[Code]...
View 5 Replies
View Related
Feb 11, 2014
I have written a CFD program that final results store at several 2D arrays. For instance, temperature of a square plate. For every point of plate i and j (i,j), I have a temperature amount. On the other hand, these temperatures change by time.
theSpace.TheCells[i, j,0].Temperature
I would like to save these data into an excel file and each sheet contains data for specific time (for example, an excel file with 50 sheets that cell number shows the coordinate of a point and the value in it is the temperature and next time step data's will be in the next sheet of current workbook )
I was thinking my code should be something like the following note, but it doesn't work:
// Implement time loop
int counter = 0;
for (int step= 1; step<= MaxSteps; step++) {
// call Excel output method
ExportData(ResultsArray,step,nx,ny);
[Code] ....
View 7 Replies
View Related
Jun 9, 2013
I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:
78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc
There's suppose to one array of companies, where each company will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in an array of structures.
My question is, how do I read the data from the file and put the data from each line into the next structure in the array of structures? I read the numbers in fine. I just use:
Code: fscanf(fp, "%f", &companyAry[i].hourlyPrices[j]);
But my program crashes when I try to read the name.
Code: fscanf(fp, "%[^]", &companyAry[i].companyName);
I'm thinking it has something to do with the fact the companyName is a pointer.
My structure looks like this:
Code:
typedef struct {
char *companyName;
float hourlyPrices[NUM_PRICES];
float avgPrice;
}
COMPANY;
View 8 Replies
View Related
Feb 14, 2015
I am at a loss with an assignment. I am supposed to read from a text file, with an input of something like this: alphaproleone,stroke,42 1 and Store it into an array of structures and then output it with each word/number starting on a new line. My current code prints out only the first part, and the "a" in alphaproleone is the actual number "21".
#include <iostream>
#include <string>
#include<fstream>
#include<iomanip>
using namespace std;
typedef struct drugtype {
string name, target;
int effectiveness, toxicity;
[Code]...
View 1 Replies
View Related
Jul 26, 2014
I have a csv file (comma delimited text file really) and I have data ,about 6400 lines, and I need to read and display data from 1 line. At the bottom is sample data from my csv file.
I want to read onto the screen the 3rd item .. ie Test User 1 and the date and time at the end (the last 2 items).
I will enter in a number .. from which I will want to retrieve the appropriate record .. in this case I would enter in 514, 563 or 874 and get back the info for that record.
This is a DOS 16 bit application .. and I know that some of the data is in memory already but I am not sure how to tell .. or how to retrieve it.
I do have the ability to save the data to the file at any time .. so I can use that to ensure that the data is current in the text file before I retrieve the data from this 1 row.
I think that it may just be easier to get from the text file ? but I am not sure.
0000,7,0,"Test User 1",53c145cb,db,5,514,514,2014/07/12,10:27:23
0001,7,0,"Test User 2",53d7f,1903,10,563,563,2014/07/24,11:04:31
0002,7,0,"Test User 3",529c,5ffb03,0,874,874,2013/12/02,09:07:58
I am a database guy .. and I am just a bit lost when it comes to C programming as such.
View 9 Replies
View Related
Apr 24, 2013
When you have constant arrays, they are stored in the programme memory space. Upon execution, they are copied into the data space for easy access. To save space in the data space, it is possible to stop it from being copied. Some compilers will deal with this by just specifying the array as const, g++ has a type modifier called PROGMEM.
In any case, the problem is that I'd like to generate some data, preferably without using an external programme to do it. Hence, my question using the template system. Now I could possibly use macro metaprogramming via boost instead of template metaprogramming, and I may have to go that route. However there are limitations, especially since its numbering system is only equivalent to an unsigned byte, which I possibly could use, but could become more messy than if I use the template system that has long long types as well as a whole slue of type safety mechanisms.
View 14 Replies
View Related
Sep 19, 2013
I got an assignment at school asking for a program that can implement functions that store, get and deletes text/binary data to a given memory area. We are supposed to make kind of like a tiny-mini OS..any links to some tutorials or explanations hon ow to understand this and be able to make a program like this on my own?
View 9 Replies
View Related
May 14, 2014
I've been using code::blocks to work on text files through fstream. But i cannot make it append using my code.
Here's my code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream dataFile;
dataFile.open("demofile.txt", ios::out);
[Code] ....
View 2 Replies
View Related
Apr 26, 2014
I'm trying append more characters to a txt file after write title of foreground window and a newline character, but after first character, the next appear after a newline. Here is result => [URL].... and here is my code:
I'm using Dev C++ 4.9.9.2 compiler.
Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
using namespace std;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
[Code]....
I have made some changes for this =>
Code:
if (str_window)
strcpy(str_window,str_window);
strcat(str_window,"
");
log(str_window);
[Code]...
Now print only first character in digitation after title of active window. Still printing wrong see => [URL]..... Word "good" as sample.
View 10 Replies
View Related
Mar 28, 2013
I am working on a c program which recovers jpeg files from .raw file of a memory card. My code is here-
Code:
#include<stdio.h>
#include<stdlib.h>
int main(void) {
File* inptr=fopen("card.raw","r")
if(inptr==NULL)
[Code]...
But while compiling it I get this error-
Makefile:2: *** missing separator. Stop.
View 2 Replies
View Related
Jun 21, 2013
the question is; Write a program that prints out the memory addresses of each element in a two-dimensional array. Check to see if the values printed out make sense to you based on the way I explained it before.
Below is the code I have done. I am having problems printing the "-" sign to keep formatting with the board when the user enter in different dimensions other than [4][4].
Code:
#include <iostream>
using namespace std;
void printTable (int x, int y) {
int **p_p_twoDimension = new int* [y];
for (int i = 0; i < y; i++) {
p_p_twoDimension[i] = new int [x];}
[Code]...
View 12 Replies
View Related