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;








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);










share|improve this question

















  • 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


















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);










share|improve this question

















  • 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














0












0








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);










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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













  • 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













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
);



);













draft saved

draft discarded


















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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴