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

How to preserve electronics (computers, tablets and phones) for hundreds of years Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) The network's official Twitter account is up and running again. What content…How might modern humans leave a message for 50,000 years?How would an aquatic race develop computers?How do we make computers care for human life?Keeping supplies for 1000 yearsWhat will cell phones become in 100 years?What body armor protects against a laser?Could a society attain 1800s technology if limited to using copper and bronze?Electricity power (magic) and electronicsScavenging metal resources in post-apocalypse after 100 yearsWhy would precursors create devices that can survive and still work after hundreds of years?

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해

Convert JSON to MapSimplest way to convert a string representing JSON array into Java array List of key value pairsHow to take String JSON values into HashMapConvert a JSON String to a HashMapCreating BSON object from JSON stringJSONObject - How to get a value ?Better Map ConstructorReading JSON from Url javaUpdate columns if input values are not null otherwise ignore and keep the existing values of column in databaseHow to store object represented as Json string into Couchbase Lite?Go JSON into MapHow do I efficiently iterate over each entry in a Java Map?Sort a Map<Key, Value> by valuesHow to round a number to n decimal places in JavaCan comments be used in JSON?How do I read / convert an InputStream into a String in Java?How can I pretty-print JSON in a shell script?What is the correct JSON content type?How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?Why does Google prepend while(1); to their JSON responses?Parse JSON in JavaScript?