C :: Writing A Struct To A File
Sep 28, 2013
I am trying to write a structure to a file. Example say the structure has two variables x and y . I want to write a function which modifies these variables and stores the modified version on a file. Such that next time I call the function . it has the values from the previous write. Here's an example of my code .
Code:
// initialize the structure struct->x = 0, struct->y = 0
File *fp = fopen("filename", "r+");
struct MYSTRUCT mystruct = (struct MYSTRUCT*)malloc(sizeof(MYSTRUCT))
//check
fread (mystruct, sizeof(MYSTRUCT), 1, fp);
// do some calculations.
fwrite(mystruct, sizeof(MYSTRUCT), 1, fp);
fclose(fp)
//return some value
}
The problem is that each time I run the program it shows the initialized value of the variables and not the value from last write. I guess the write isn't successful because when I open in w+ mode. i get the error file could not be opened and then i have to delete the file and re create it....
View 3 Replies
ADVERTISEMENT
Feb 6, 2014
I am writing a simple file/text parser to read a config file for some code I am working on. It's dead simple and not particularly smart but it should get the job done. The code reads a config file:
Code:
readlength=2500000
start=0
finish=25000000
cutoff=20000
samplingfreq=250000
poles=10
filterpadding=500
}
[code]....
Here is where it gets wierd. You'll notice that there is an unused variable (filepath) in the config struct. This variable is not referenced or used anywhere in the code, ever. Yet if I comment out the declaration of char filepath[1024], the code segfaults partway through the read_config() function.
My best guess is that there is a buffer overflow elsewhere and it just so happens that the memory allocated for filepath happened to be there to catch it up until now, but I can't work out where it might be happening. With the declaration commented out, the read_config() function gets as far as reading the "padding" variable before it crashes. Yet when the declaration is there, then all the variabled are read correctly and everything seems to work.
View 10 Replies
View Related
Aug 31, 2014
One of my class assignments is to create a program that receive a .txt file containing a students name and their grades as follows: John K. 99, 87, 57, 89, 90, 95 Amanda B. Jones 100, 88, 76, 99, 86, 92 etc.. The number of students is unknown until run time. You have to take those grades and average them weighing the first (4) at 10% a piece and the next (2) at 15% each and the final at 30%. Then return an output file with the students name and their letter grade A,B,C,D,F based on their computed score. In addition, on screen it needs to display the average scores for each Q1, Q2, etc. as well as the minimum and maximum for each test on the screen. I am having a hard time in assigning the scores to a variable so that they can then be computed as an average and then used to determine a letter grade. I have begun to write the code and am a bit stuck..here's what I have so far:
Code:
//
// main.c
// Final Exam
//
[Code].....
The problem I'm having now is how to go about passing the grades to the function computeGrade and then compute the average and return that to the function.
View 4 Replies
View Related
Mar 28, 2013
Can i edit records stored in file without rewriting whole file again .?
View 1 Replies
View Related
Aug 5, 2013
How to save the struct to file and then read it back from the file ?
Code:
#include <conio.h>
#include <stdio.h>
struct student {
int person;
int egn;
float AvergeGrade;
[Code] ....
Have average grade: %f", person[i].FirstName, person[i].LastName, person[i].egn, person[i].AvergeGrade);
}
//Save to file
getch ();
}
View 2 Replies
View Related
Sep 9, 2014
I am executing below sample program
Code: #include<iostream.h>
#include <fstream.h>
using namespace std;
int main()
[Code]......
when i am counting the lines using wc command it is giving 1 instead of 2.
View 3 Replies
View Related
Nov 3, 2014
I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits
Code:
class BitInput {
istream& in; // the istream to delegate to
char buf; // the buffer of bits
int nbits; public:
BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
[Code] ....
View 4 Replies
View Related
Dec 11, 2011
I have a question about an issue I am having on my final project. Within my ItemEntry.cs form, I am trying to get the application to append the already existing .txt file, rather than prompt the user to replace it. I can't seem to get the StreamWriter and FileStream to allow the user to write to the file.
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
[Code] .....
View 1 Replies
View Related
Sep 19, 2013
I have a text file containing 500 signed decimal numbers. My task is to read each entry and convert into a 16-bit 2's complement representation (binary number) and write into the another text file.
View 2 Replies
View Related
Oct 4, 2014
I have a problem writing new line in a file. So far, this is my code.
Code:
fprintf(f, (ctime(&mytime)));
fprintf(f, "%s %s %d %d", item, brand, price, amount);
fprintf(f, "
");
fclose(f);
View 13 Replies
View Related
Mar 19, 2013
Code:
#include <stdio.h>
struct hardware{
int recNum;
char toolName[30];
int quant;
double cost;
[Code] .....
I'm having issues with my functions working properly. I'm not sure where I'm going wrong with them and if i'm using the correct mode for the fopen.
View 6 Replies
View Related
Oct 11, 2014
I'm trying to write the value of xcord to the file, but it's come out as jiberish like š™ for some reason.
#include <iostream>
#include <Windows.h>
using namespace std;
[code]....
View 2 Replies
View Related
Apr 30, 2013
Is there a way to read and write to the same file?
I'm writing a game program and I want to save the score at the end of the game to a txt file. The txt file already contains other game scores. How do I store the score without overwriting the previous scores.
I'm just using basic fstream.
View 2 Replies
View Related
May 28, 2013
I want to write a program in cpp which will create a file and write some classes into it so that whenever i will execute that program i will have the auto generated file. How to do it....
View 1 Replies
View Related
Aug 22, 2014
suddenly nothing is written to my file
void taglessLine(char line[],ofstream& buffer){
buffer.open("buffer.txt", ios::out);
int len=strlen(line);
[Code].....
View 2 Replies
View Related
Nov 3, 2014
I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits
public:
BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
/** Read the next bit from the bit buffer.
* Return the bit read as the least significant bit of an int.
*/
int readBit(){
int i;
if(nbits == 8){
buf = in.get();
[Code] .....
View 2 Replies
View Related
Sep 12, 2014
I've got an issue with my code; whenever I add the " " for a newline, a newline is not actually printed onto the file.
Here's my code:
/*
Description: Lists all the files & other directories in the directory passed through argv.
*/
/*
Importing the required headers.
Dirent.h for the dirent struct.
Stdio.h for printf.
Conio.h for File input/output.
*/
#include <dirent.h>
#include <stdio.h>
#include <conio.h>
int main ( int argc, char *argv[] ) {
// Checking that the user has given the correct number of arguments.
[Code] .....
The solutions I've tried are:
Trying to add the ASCII character for a carriage return (13) and newline (10), but it didn't work.Using fputs() and fprintf() - both mentioned in the code.
View 8 Replies
View Related
Mar 31, 2013
I want to do a basic thing but for some strange reason there is something not working.
I need to write to a file but I'm handling all the code in a callback function, which in turn is declared in a class. Here is the architecture of my code:
Code:
int main (int argc, char **argv) {
ImageConverter Aclass;
while(1); //infinite loop
return 0;
[Code] ....
But this doesn't work, the file gets created but it allways overwrites itself which is not what I need to do.
What I am doing wrong? Is it because i am constantly declaring outfile?
View 10 Replies
View Related
Oct 20, 2014
I'm currently finishing writing some small application. I want to be able to log important information about the program execution to a logfile, and I have several questions.
First of all - I'd prefer to make the part that logs information to a file separate from the code I've already written. So, what interface should I expose to the rest of the program? Is one function void log(const char*); enough?
Another thing that came to my mind; my program runs two threads, and I want to be able to write to the log file from both threads, so the question is: Should I make sure that the writing to the file is mutually exclusive?
And if so, whose responsibility is it to make the logging to the file thread-safe? The actual part that does the logging (void log(const char*) for that matter), or the parts of the program that calls log(const char*) ?
And lastly, and probably less importantly, where is it customary to save the logfile? (the user's home folder maybe?)
View 3 Replies
View Related
Mar 21, 2013
Im programming client/server app that client provide the file name then the server send it to client then the client will save it ..this is part of code in client
Code:
char buffer[1024];
printf("FIle is being downloaded ...
");
printf("%s
",buffer);
}
[code],...
So i have 2 problems ::
1st one is when i write to file the file permission i cant define it with data type mode_t ,so the file does not open at all after creation...
2nd one is: the data in buffer is less than 1024 ,the data wrote to buffer but with garbage data . How to make the file read only the real data with garbage ??
View 5 Replies
View Related
Oct 30, 2014
So I'm trying to take some information that a user inputs and to then write it into a .txt file. The user would input a student ID followed by 4 quiz grades. I need to use nested loops for the input, a while loop for the outer and a for loop for the inner.
The data in the .txt should would look like:
studentID quiz1 quiz2 quiz3 quiz4
studentID quiz1 quiz2 quiz3 quiz4
etc.
My problem is I'm not sure how to structure the code. I have very few examples I'm working with to understand what I'm working with.
View 2 Replies
View Related
Apr 15, 2014
I have a character pointer that points to 4000 bytes of valid data. I need to write these valid contents pointed to by this pointer into the file. I am looking for the most optimized way of doing this. I am using the below logic which seems trivial. Any better approach to accomplish the same?
/* length is the number of valid bytes of data */
void parse_contents(const char *data, int length) {
int j = 0;
char path[1024] = "/tmp/data.xml";
FILE *fp = NULL;
fp = fopen(path, "wb");
[Code] ....
View 2 Replies
View Related
Aug 16, 2013
I a want to write a code to convert a string into binary data for that i wrote a code its working perfectly but there is one problem , some of the binary data is written in 7bit and i want to convert it to 8 bit by adding 0 to the last.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code]....
View 2 Replies
View Related
Oct 16, 2013
I have a file of data where each line consists of 4 parameters (degree, alpha, beta and x) and a value corresponding to these, organized in increasing order of the parameters. Here's the test file I'm using:
0 1 1 0.5 3.5
1 1 1 -0.5 -0.5
1 2 0 0.2 4.5
1 3 0 0.2 -4.5
2 0 0 1.5 2.1
2 1 0 0 5.6
My code is supposed to look for a certain set of parameters (currently set to 1 2 1 1.5, which is not contained in the file) and its value. If it's there (i.e. the file contains the value for the right parameters) the program is done. If not it should calculate the value using a function JacobiPoly() and insert the wanted parameters and corresponding value in the appropriate place in the file.
I have a code in which I think everything works except the writing to file. I use an fstream so I can both read and write and open it with ios::out||ios::in. I'm also aware I need something similar to fseek in between input and output but according to [URL] .... reading and writing to the same file using the same fstream, seekp() should suffice. I'm using seekp(), but the output (in the form inout << (...)) doesn't work.
Here's the code:
fstream inout;
inout.open("inouttest.dat", ios::out | ios::in);
int degree=1, readDeg;
double alpha=2, beta=1, x=1.5, value, readAlpha, readBeta, readX, readVal;
bool foundVal=false;
streampos beforeRead=(streampos)0;
[Code] ....
View 3 Replies
View Related
Mar 27, 2013
basic code for writing output of a program to a file? I can't seem to find it online and I need it to complete a project.
View 2 Replies
View Related
May 21, 2013
Here are the requirements for a project I am working on.
Write a function that will write only unique ip addresses to a file (just the ip address, not the entire line).
Write a function that will search for an ip address and display all page requests for that ip address.
The ip is pulled from a "weblog.txt" file which has been put into a vector and sorted. I am having trouble with the loop to pull only unique ip addresses from the vector.
Code:
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<algorithm>
void nonmember();
void sortnonmember(std::vector<std::string>& webvector);
[Code] .....
View 1 Replies
View Related