Writing/Reading a struct from a .txt fileHow do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?How do I create a Java string from the contents of a file?When to use struct?How do I tell if a regular file does not exist in Bash?Find and restore a deleted file in a Git repositorytypedef struct vs struct definitionsHow do I create a file and write to it in Java?Find all files in a directory with extension .txt in PythonCorrect way to write line to file?Delete a file or folder
Cops: The Hidden OEIS Substring
Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?
Does Google Maps take into account hills/inclines for route times?
Why would guns not work in the dungeon?
How can an advanced civilization forget how to manufacture its technology?
Filtering fine silt/mud from water (not necessarily bacteria etc.)
Package, repository and installation process in Linux
Is it rude to tell recruiters I would only change jobs for a better salary?
Why did the Japanese attack the Aleutians at the same time as Midway?
Would letting a multiclass character rebuild their character to be single-classed be game-breaking?
PIC12F675 GP4 doesn't work
What is temperature on a quantum level
As the Dungeon Master, how do I handle a player that insists on a specific class when I already know that choice will cause issues?
Bronze Age Underwater Civilization
How does a Potion of Poison work?
Would dual wielding daggers be a viable choice for a covert bodyguard?
How can one write good dialogue in a story without sounding wooden?
How to check the quality of an audio sample?
What explains 9 speed cassettes price differences?
QGIS Welcome page: What is 'pin to list' for?
How do I take a fraction to a negative power?
Were there any new Pokémon introduced in the movie Pokémon: Detective Pikachu?
How can I get both Giga Drain and Mach Punch on Breloom?
How do Windows version numbers work?
Writing/Reading a struct from a .txt file
How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?How do I create a Java string from the contents of a file?When to use struct?How do I tell if a regular file does not exist in Bash?Find and restore a deleted file in a Git repositorytypedef struct vs struct definitionsHow do I create a file and write to it in Java?Find all files in a directory with extension .txt in PythonCorrect way to write line to file?Delete a file or folder
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to write an array of structs to a file, then read that file into an empty array of structs and print out the variables within each struct element of the array.
my code is below, currently I just have it to try to print the first element, but nothing is printed out to the screen.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
char name[1024];
int price;
game;
void main()
FILE *filename;
filename = fopen("file.txt","w");
game library[3]; //creation of our game library
//initializing the array to be written, 3 structs inside this array should be written to the file in sizeof(game).
strcpy(library[0].name,"Minecraft");
strcpy(library[1].name,"Opium");
strcpy(library[2].name,"Devil");
library[0].price = 10;
library[1].price = 20;
library[2].price = 30;
fwrite(library,sizeof(game),3,filename);
fseek(filename,0,SEEK_SET);
game second[3];
printf("test");
fread(second,sizeof(game),3,filename);
printf("element 0 is %s $%d",second[0].name,second[0].price);
fclose(filename);
c struct file-io
add a comment |
I'm trying to write an array of structs to a file, then read that file into an empty array of structs and print out the variables within each struct element of the array.
my code is below, currently I just have it to try to print the first element, but nothing is printed out to the screen.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
char name[1024];
int price;
game;
void main()
FILE *filename;
filename = fopen("file.txt","w");
game library[3]; //creation of our game library
//initializing the array to be written, 3 structs inside this array should be written to the file in sizeof(game).
strcpy(library[0].name,"Minecraft");
strcpy(library[1].name,"Opium");
strcpy(library[2].name,"Devil");
library[0].price = 10;
library[1].price = 20;
library[2].price = 30;
fwrite(library,sizeof(game),3,filename);
fseek(filename,0,SEEK_SET);
game second[3];
printf("test");
fread(second,sizeof(game),3,filename);
printf("element 0 is %s $%d",second[0].name,second[0].price);
fclose(filename);
c struct file-io
2
you are opening the file in the "w" mode. You need to open in the "w+" mode to allow reading and writing.
– Rishikesh Raje
Mar 26 at 4:31
ah easy mistake, my code works now thanks! @RishikeshRaje
– Fireboy653
Mar 26 at 4:42
add a comment |
I'm trying to write an array of structs to a file, then read that file into an empty array of structs and print out the variables within each struct element of the array.
my code is below, currently I just have it to try to print the first element, but nothing is printed out to the screen.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
char name[1024];
int price;
game;
void main()
FILE *filename;
filename = fopen("file.txt","w");
game library[3]; //creation of our game library
//initializing the array to be written, 3 structs inside this array should be written to the file in sizeof(game).
strcpy(library[0].name,"Minecraft");
strcpy(library[1].name,"Opium");
strcpy(library[2].name,"Devil");
library[0].price = 10;
library[1].price = 20;
library[2].price = 30;
fwrite(library,sizeof(game),3,filename);
fseek(filename,0,SEEK_SET);
game second[3];
printf("test");
fread(second,sizeof(game),3,filename);
printf("element 0 is %s $%d",second[0].name,second[0].price);
fclose(filename);
c struct file-io
I'm trying to write an array of structs to a file, then read that file into an empty array of structs and print out the variables within each struct element of the array.
my code is below, currently I just have it to try to print the first element, but nothing is printed out to the screen.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct
char name[1024];
int price;
game;
void main()
FILE *filename;
filename = fopen("file.txt","w");
game library[3]; //creation of our game library
//initializing the array to be written, 3 structs inside this array should be written to the file in sizeof(game).
strcpy(library[0].name,"Minecraft");
strcpy(library[1].name,"Opium");
strcpy(library[2].name,"Devil");
library[0].price = 10;
library[1].price = 20;
library[2].price = 30;
fwrite(library,sizeof(game),3,filename);
fseek(filename,0,SEEK_SET);
game second[3];
printf("test");
fread(second,sizeof(game),3,filename);
printf("element 0 is %s $%d",second[0].name,second[0].price);
fclose(filename);
c struct file-io
c struct file-io
asked Mar 26 at 4:02
Fireboy653Fireboy653
14 bronze badges
14 bronze badges
2
you are opening the file in the "w" mode. You need to open in the "w+" mode to allow reading and writing.
– Rishikesh Raje
Mar 26 at 4:31
ah easy mistake, my code works now thanks! @RishikeshRaje
– Fireboy653
Mar 26 at 4:42
add a comment |
2
you are opening the file in the "w" mode. You need to open in the "w+" mode to allow reading and writing.
– Rishikesh Raje
Mar 26 at 4:31
ah easy mistake, my code works now thanks! @RishikeshRaje
– Fireboy653
Mar 26 at 4:42
2
2
you are opening the file in the "w" mode. You need to open in the "w+" mode to allow reading and writing.
– Rishikesh Raje
Mar 26 at 4:31
you are opening the file in the "w" mode. You need to open in the "w+" mode to allow reading and writing.
– Rishikesh Raje
Mar 26 at 4:31
ah easy mistake, my code works now thanks! @RishikeshRaje
– Fireboy653
Mar 26 at 4:42
ah easy mistake, my code works now thanks! @RishikeshRaje
– Fireboy653
Mar 26 at 4:42
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349657%2fwriting-reading-a-struct-from-a-txt-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349657%2fwriting-reading-a-struct-from-a-txt-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
you are opening the file in the "w" mode. You need to open in the "w+" mode to allow reading and writing.
– Rishikesh Raje
Mar 26 at 4:31
ah easy mistake, my code works now thanks! @RishikeshRaje
– Fireboy653
Mar 26 at 4:42