C :: Read From File Into Structure?
Nov 24, 2013
When I tried to run my code I keep getting a "Segmentation fault".
I am trying to write a code that read from a file and put the data into a structure.
The file look like the following:
2001,ABBIGAEL,5
1994,ABBIGAIL,5
1996,ABBIGAIL,8
1997,ABBIGAIL,13
The file have 31 lines.
Code:
#include <stdio.h>
#include <string.h>
int main() {
/* Define a daydata structure */
typedef struct {
[Code]....
View 4 Replies
ADVERTISEMENT
May 19, 2014
I am trying to use arithmetic on structure members read from separate text files into separate functions. I'm trying to do the math in the main function after calling the other two functions. I'm trying to divide nato_attack by pact_defence.
The warning I'm getting is: both are being used uninitialized in this function.
The goal is to read/write the info in the text files and to use them as any other variable, i.e. add, subtract, etc. in other locations in my code, i.e. functions, modules.
I "borrowed" most of this code from a youtube lesson and modified it for my needs.
Below is the contents of the two text files and my code.
This is for NATO
6
12 4th_mech_div mech div 14 16 7
12 5th_mech_div mech div 10 8 7
12 3rd_mech_div mech div 5 6 6
12 1st_mech_div mech div 4 6 7
12 2nd_mech_div mech div 12 14 7
12 6th_mech_div mech div 8 12 6
This is for Pact
3
10 1st_Guards armor div 12 8 6
10 2nd_Guards armor div 12 9 6
10 3rd_Guards mechanized div 10 9 6
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *unit_id;
char *unit_type;
char *unit_size;
[Code] ....
View 9 Replies
View Related
Dec 10, 2014
I tried to make the program read from a file text the first name or last name but o cant seem to get it. i tried alot of different ways. how can i get the array of structure code to read form a file?
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
const int NUM_STUDENTS = 17;
const int NUM_QUIZZES = 10;
const int NUM_EXAMS = 5;
[Code] .....
View 14 Replies
View Related
Aug 21, 2014
I would like to write a complete structure array to a file and read it back, recovering all the data. I have tried the following:
Code:
#include <stdio.h>
#include <string.h>
#define NUM 256
const char *fname="binary.bin";
typedef struct foo_s {
int intA;
int intB;
char string[20];
[Code]...
//---------------------------------------------------- but the mac field is reading back some random value repeatedly. Why is that? And how do I fix this?
View 8 Replies
View Related
Feb 5, 2015
writing this program where you can search a structure array for existing elements, add new elements to the structure array, and find and display entire elements of the array structure and all.
Right now I am stuck on adding new elements to the structure array.
#include <iostream>
#include <string>
#include <iomanip>
[Code]....
Any methods to displaying the a array structure or editing it ....
View 6 Replies
View Related
Apr 6, 2014
My program is designed to read input from the user and then store that input in a structure variable. Whenever i use the cin.getline() function, it comes up with a semantic issue. Here is the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int DESCRIPTION_SIZE = 100, DATE_SIZE = 20;
struct inventory {
string description[DESCRIPTION_SIZE];
[Code] .....
I left out the other functions. The error reads "no matching member function for call to 'getline'.
View 6 Replies
View Related
May 15, 2013
I want to read the contents of a file block (512 bytes) by block using low I/O read statements. Each record is 64 bytes long and has a pre-defined structure. The first 4 bytes are an unsigned integer; the next 20 bytes are ascii text, etc.
I have a buffer which I can access with buf[0] to buf[63] to read the first record and then buf[64] to buf[127] for the second, etc. However, I was wondering how to map a record so that I can refer to an integer as an integer and a float as float, etc. I can't create a struct and move the 64 bytes to it, as I will have alllignment/padding problems.
What is the standard way to deal with records in C?
View 5 Replies
View Related
Aug 27, 2013
I am trying to run a programme implementing a function with structures in c... which is:
#include<stdio.h>
#include<conio.h>
struct store {
char name[20];
float price;
int quantity;
[Code] .....
View 1 Replies
View Related
Mar 11, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX 200
[Code] .....
I made a txt file which contains a necessary information into my project file and tried to read and print it. However, seems like my program is not reading my file at allI named my file as student
View 2 Replies
View Related
Dec 7, 2014
Why doesn't this compile?
struct hi(){
void other();
}histructure;
void hi::other(){
std::cout << "Hi!" << std::endl;
[Code] ....
Makes no sense the structure is written before the structure member function is called so why is there compile errors ??...
View 3 Replies
View Related
Jul 17, 2013
How to structure the input of one .dat file and outputting into another one. I have to loop through a file structured "doe, john, 1500", check for errors and then increase the mp by 50. You also have to do this through using a person class too. I think I need to use a while loop to go to eof and use getline to input correctly, but im not sure how to structure it.
Data file:
Horowitz,Jake,1200.24
Flabitz,Bubba,1712.38
Blackwell,Sharon,1872.93
Molerat,Rufus,501.00
Goodall,Jane,1567.43
Sommerset,William,1359.57
[Code] ......
View 4 Replies
View Related
Nov 21, 2013
I am trying to take a Text File mydata.txt and import it into a structure however when I try to run this code it just closes out.
Code:
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#include<ctype.h>
struct hard_disk{
char name[18];
double size;
[Code] .....
View 4 Replies
View Related
Mar 28, 2014
I'm trying to insert a structure into a binary file, then read it back out into a "temporary" structure and output some values.
#include <iostream>
#include <fstream>
using namespace std;
struct b_tree_node {
int num_keys; // number of keys in this node
int key_val[3]; // key values (0 to num_keys - 1)
int child[4]; // child file addresses (0 to num_keys)
[Code] ....
View 12 Replies
View Related
Mar 27, 2014
I'm trying to save a structure to a binary file, then read it out of the binary file into a new structure. I am unsure what is happening but it is not correct.
#include <iostream>
#include <fstream>
using namespace std;
[Code]....
View 2 Replies
View Related
Mar 18, 2015
I am doing a project in C++ - a simple data base (with "one table"). I want to build that data base using a LIFO or FIFO list, but this doesn't matter here
The only problem I have is that the structure of the data base is not "locked" in the code of the program, but when the program starts, it is "loaded / implemented" from a "configuration file" called "data.txt".
So the user, before she or he runs my program opens the file "data.txt" and writes the structure of the data base that se/he wants to create. This means that one time my program can be used to create a data base of students with fields such as "name / surname / gender / age / idnumber / university" but the next time it can be used for a data base of CD's with fields like - "author / release date / no. of tracks / etc.".
How to do that "loading" of a structure of a data base from a file, when the user runs my data-base creator (my C++ program).
View 1 Replies
View Related
Mar 18, 2015
I am doing a project in C++ - a simple data base (with "one table").
I want to build that data base using a LIFO or FIFO list, but this doesn't matter here . The only problem I have is that the structure of the data base is not "locked" in the code of the program, but when the program starts, it is "loaded / implemented" from a "configuration file" called "data.txt".
So the user, before she or he runs my program opens the file "data.txt" and writes the structure of the data base that se/he wants to create. This means that one time my program can be used to create a data base of students with fields such as "name / surname / gender / age / idnumber / university" but the next time it can be used for a data base of CD's with fields like - "author / release date / no. of tracks / etc.".
How to do that "loading" of a structure of a data base from a file, when the user runs my data-base creator (my C++ program).
View 2 Replies
View Related
Mar 5, 2014
I want to define enumeration inside a structure and use it in some other file
for eg
header.h
typedef struct dummy {
typedef enum {
ZERO,
ONE,
MAX
} NUMBERS;
// some member variables
} DUMMY; // end of the structure
otherfile.cpp
in this file i want to access both enumerations(for eg) ONE as well as the type NUMBERS.
View 1 Replies
View Related
Feb 8, 2014
I am trying to write a program for a library system that allow stuff to add, remove, view and delete customer. i try to use file to store data and i open the file in mode read then i store then i put the content of the file into a structure. now the problem started it is only showing me haft of the content and here is the coding.
#include <stdio.h>
# include <windows.h>
# include <stdlib.h>
#include<string.h>
#include<conio.h>
void search(int s,struct books eli[20]);
void view(int x,struct books eli[20]);
[Code] .....
View 1 Replies
View Related
Jan 23, 2013
I need to compute some values, place them in a structure containing two vector variables, and save them in a CSV file. Right now, I can't even generate a file. I wrote the structure definition and file writing code in a header file (after first borrowing it). I have a "button" that is supposed to trigger all of the activity. I'll attach the code below:
HEADER
________-----_______------
#ifndef EINSTEIN_H
#define EINSTEIN_H
#include <stdio.h>
struct SizeAll{
int year;
double R;
};
int write_to_file(int count, struct SizeAll *data, char const *fileName)
[code].....
My grasp of pointers and how and when to use them evaporated long ago.
View 14 Replies
View Related
Oct 7, 2014
Is it possible to assign a value to structure member inside the structure.like.....
struct control{
char tbi:2 =0;
char res:1 =0;
};
View 6 Replies
View Related
Aug 5, 2014
I'm having trouble reading my data from a .txt file into a structure of the format shown in my code. I've made my student database in the program below based on user input and I didn't have a problem with that, but now it's come to input from a file it's making it difficult.
My three tasks are:
(1) A table containing 1 row per student, containing the student ID number and all of the student's marks.
(2) Another table, containing 1 row per student, containing the student ID number and the average mark obtained by that student.
(3) The average mark for each subject.
The assumptions to be made are: The student ID can begin with a zero, and should therefore be read in as a string.The test data will contain 20 students but the program should be able to deal with up to 100 students.Assume there are no more than 4 different subjects.
So based on the first assumption I've arranged the data in the file in an order in which the student ID begins with a zero:
003953 Computing 38
003953 English 88
003953 Mathematics 29
003953 Physics 83
073241 Computing 63
073241 English 99
073241 Mathematics 32
073241 Physics 73
...for twenty students, 80 lines of data. Now, on the assumption they must be read in as strings, this is what's making it tricky to store in the structure because, I've got 80 ID numbers but 20 repeat themselves 4 times. Once I've got this data in the structure below the tasks I won't have a problem with because I can just base it on a user input program but the data's already stored instead.
Below is my code for user input associated with task (1). In this example the IDs are stored as ints but for the file they will be strings. It compiles fine, displays the data as shown in the assignment sheet, but I don't know how to get the data into my structure. I can store the data in a structure of three arrays using fscanf() no problem, but it's not very "workable" for what I need to do with it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
[Code].....
View 11 Replies
View Related
Feb 20, 2015
I have a structure
struct Wine
{
string wineName;
int vintage;
int rating;
double price;
};
how can i store the file data below in an array with the structure type???
Dow Vintage Port ;2011;99;82
Mollydooker Shiraz Carnival of Love ;2012;95;75
Prats & Symington Douro Chryseia ;2011;97;55
Quinta do Vale Meão Douro ;2011;97;76
Leeuwin Chardonnay River Art Series ;2011;96;89
View 1 Replies
View Related
Apr 20, 2014
I need to make a program which reads multiple lines from a text file and stores that information in a vector of structs(the vector class template also needs to be custom made).
One of my requirements is to have a class dedicated for I/O for the text file. At the moment I can't seem to get a way to input the data from a file into a vector from an InputOutput class. This is my code:
InputOutput.h
#include <ostream>
#include <fstream>
#include "Share.h"
#include <string>
#include "Vector.h"
class InputOutput {
[Code] ....
This is the menu selection function in the menu.cpp where i figured i would call the Input file and store it from case 1. I think I'm doing it wrong though. Is there a better way of doing this because at the moment i am getting some errors such as error LNK2019.
void Menu::UserMenuSelection() {
Vector<Share> shares;
std::ifstream infile;
switch (menuOption) {
[Code] ....
View 2 Replies
View Related
Sep 5, 2014
have to do an election program in C.
There are 7 candidates and 365 votes in total. I need to do this using an array of structures. I need to read from a text file each of the names of the candidate and the number of votes they get. At the end i need to output the winner of the election.
Here is a sample of my code so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct candidates {
char name[20];
int votes;
[code]....
Here is a sample of my text file:
Robert Bloom
John Brown
Michelle Dawn
Michael Hall
Sean O’Rielly
Arthur Smith
Carl White
1 2 4 5 1 2 3 4 4 1 2 3 7 4 4 5 3 7 7 7 7 7 7 7 7 7
Each candidate gets +1 vote for their number electionCandidate[0] for each one he gets one vote and so on for the rest. 365 voters in total.
I was able to input the name for each Candidate from the text file. Now the problem is putting each vote to the corresponding candidate. Also any vote that is above 7 is a spoilt vote which i am trying to count in the above code. The code compiles but it crashes.
I am using while(!feof) but it seems that its not working or this is not the correct way.
View 6 Replies
View Related
Mar 17, 2013
how I can create a structure that pointing to another different structure. And also passing structure to function.
View 3 Replies
View Related
Mar 15, 2013
The Objective Of This Program Is To Create A File To Write Text And Read Back The File Content. To Do That I Have Made Two Function writeFile() To Write And readFile() To Read.The readFile() function works just fine but writeFile() doesn't.
How writeFile() function Works? when writeFile() function Execute It Takes Characters User Type And When Hit Enter(ASC|| 10) It Ask "More?(Y/N)" That Means What User Want? Want To Go Next Line Or End Input?
If "Y" Than Inputs Are Taken From Next Line Else Input Ends.
But The Problem Is When Program Encounters ch==10 It Shows "More?(Y/N)" And Takes Input In cmd variable.If cmd=='Y' I Mean More From Next Line Than It Should Execute Scanf Again To Take ch I Mean User Input.But Its Not!!! Its Always Showing "More?(Y/N)" Again And Again Like A Loop.
Code:
#include <stdio.h>
void writeFile(void);
void readFile(void);
int main(){
[Code].....
View 5 Replies
View Related