C++ :: Reading In Values From Txt Files
Dec 22, 2014
I cant seem to read stuff in from my students.txt file which contains this
19992195 John Smith 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
20051195 Mark Dobson 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
20063196 Paula O'Reilly 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
20053344 Aoife Cleary 20 05 1995 09 09 2011 15 05 2014 55 65 72 58 80 45 40 45 66 78 88 81
It has something to do with the lines at the bottom of the code. It doesnt cout anything. The program just crashes upon execution. First, it asks me the amount of students i want to handle. I enter '1'. Then it asks me if i want to read in from external file and i enter 'Y'. then the exe just crashes
#include <iostream>
#include <cstddef>
#include <cstdlib>
[Code]......
View 3 Replies
ADVERTISEMENT
Dec 11, 2014
I have written in a binary file using fstream an using sort value.
unsigned shortwidth=50,height=50;
file.write((char*)&width,sizeof(width));
file.write((char*)&height,sizeof(height));
When i try to read it back from fstream again there are some symbols (binary obviously). How can i get my values back? I want to read those symbols and in a way to convert them to my old width and height values.
View 6 Replies
View Related
Feb 26, 2013
How do I read a .pdf file using C++? When I try to open it using myfile.open ("example.pdf"); but all I see is some Crazy Symbols. Is there any way to open ?
View 3 Replies
View Related
Feb 20, 2015
I have to write a program that sorts names and grades from a file and output them to another.
For example:
Name: Peter Parker
Exam 1: 95
Exam 2: 90.625
Exam 3: 91.20
Name: Mary Smith
Exam 1: 65
Exam 2: 79.1234
Exam 3: 70.24
Becomes something like this in the output file:
Name: Parker, Peter
Average Score: 92.28
Grade: A
Name: Smith, Mary
Average Score: 71.45
Grade: C
I know I'm supposed to read the whole file, but I'm getting really confused on how to take the name of each student separately without recording Exam 1, 2, and 3. I'll be able to do the average score and grade on my own.
View 2 Replies
View Related
Jan 28, 2014
Is there a way to indicate how many records exist in a given file? For example, vectors have the vector.size() command to show the number of given elements. Is there a such command for files and records?
View 6 Replies
View Related
Oct 3, 2014
The directions are to write a program that reads sorted integers from two separate files and merge the contents of the file to an output file (third file). The only standard output will be any errors to be reported and a “FINISHED” statement after all items have been processed.
file1.txt
2
4
6
8
10
file2.txt
1
5
11
12
15
Output.txt
1
2
4
5
6
8
10
11
12
15
This is the code I have so far, but it is not working, and I have put the two txt files in the same directory as my .cpp file. It is not working though still. What have I done wrong and how can I fix it to read these integers from the two numbers and merge the contents into a third file?
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int num1;
int num2;
ifstream inputFile;
[Code] ....
View 5 Replies
View Related
Mar 24, 2014
My question portion is attached. The code I have written is below.
#define _USE_MATH_DEFINES // for C++
#include <cmath>
#include <iostream>
#include <iomanip>
[Code].....
View 3 Replies
View Related
Jan 16, 2015
I'm messing around with reading and writing files. The first file creates a small txt file. Simple enough
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string name;
string desc;
[Code] ....
It does what it should. It creates a text file "items.txt" .... It reads as such:
dagger,a dagger,15,10,0,1,3,1,0,0,0,1,0,0,0,0,0,1
The second file is meant to read the file and place the data back into the variables. This happens, but the data crams itself into the first variable, and the rest of them collect the trash that's in memory.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string name,desc;
[Code ....
I need to get "dagger" into name, "a dagger" into description, and each value with their perspective variable. I'm sure I need some type of "separator". Hopefully I can use the comma. Before it's over, I will have about a hundred items that will need to be read into a class of items.
View 4 Replies
View Related
Jun 11, 2014
My assignment was to take numbers from two different files, and merge them into a third file in numerical order. I have everything working well, but the output shows the lowest number everytime and i'm not sure why. logical error ....
Sample:
File1 File2 File3
10 12 10
20 25 12
30 37 10
40 49 25
10
37
10
49
Here is my code as well
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
ifstream File1, File2;
ofstream Output;
[Code] ....
View 14 Replies
View Related
Apr 19, 2013
Code:
struct magic {
char windowsheader[2];
} magicnumbers;
struct bmp {
unsigned int sizeofthefile;
short int applicationspecific1;
[Code] .....
When I run this program, seems like it is working,at least I am sure that it gives the right file size. But if I don't use struct magic and instead use:
Code:
struct bmp {
char windowsheader[2];
unsigned int sizeofthefile;
short int applicationspecific1;
short int applicationspecific2;
//same lines with above
} bitmap_header;
And if I don't use first fread:
Code:
fread(&magicnumbers,sizeof(magicnumbers),1,fp);
Then the values which structure "bmp"'s variables get become wrong. Of course I change the related printf to:
Code:
printf("
windowsheader:%d %d
",bitmap_header.windowsheader[0],bitmap_header.windowsheader[1]);
Why does the problem happen,what is the difference between them?Why do I have to use separate structs?
View 2 Replies
View Related
Nov 27, 2013
reading fasta formatted files. Usually ppl from computation biology know what that is but for those that are not here is a quick guide :
Fasta formatted file:
Code: >header
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseq
>header1
seqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseqseq
seqseqseqseqseqseqseqseqseqseq
... so a very simple file format. and what i need to do is to read that into memory (vector of strings) so that header info and seq info is in two different vectors. for that i am using the following function :
Code:
sequence and headers vectors are class variables:
template <typename TStr>
template <typename FS>
void FastaSeq<TStr>::ReadFasta(FS& fs){
string line;
string seq;
while (fs >> line){
if(line.c_str()[0] == '>'){
[code].....
this code does what is suppose to do but it is EXTREMELY slow. it takes me exponentially more time to read the seq in memory then to process it. when compared to c style alternative that reads character by character it takes 13 sec with this function to do the same job that my c function does in 0.03 sec. How to improve upon this function to make it comparably fast (also i am using optimization) ?
View 10 Replies
View Related
Jan 23, 2013
I want my program to be able to read text files in the format:
number number
number number
number number...
and so on, so there are two columns of values. The problem I'm having is, there are a lot of numbers in the file, and it can vary. The program needs to read the numbers, put one column into one array, and the other in another, perform some operations on it all, and eventually pop out two different arrays. In other words, after I've got these arrays, I don't need them for much longer. I was hoping I could dynamically allocated some arrays to store the numbers and just free them as soon as I'm done with them.
If the file wasn't of such variable size or if it was guaranteed to be under a certain number of variables, I would have used:
Code:
while ( fscanf(input, "%lf %lf
", &col_1[], &col_2[]) == 2 )
{
no_rows++;
}
The problem is I want to allocate "no_rows" as the array size, which I don't have until after I have read the file.
View 9 Replies
View Related
Sep 7, 2014
I use this header dirent.h to get list of files in directory
[URL] .....
I am using code::blocks on windows XP.
Directory "kernels_source" is placed in project's directory where I am running the code.
Code:
#include "include/types.h"
#include "include/gaussian.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
[Code]....
View 10 Replies
View Related
Dec 10, 2013
program that I am working on. I want to use fgets() in my program so I could handle multiple words from a text(to be able to handle spaces). I get a weird result when running the program.
Code: #include <stdio.h>
#include <stdlib.h>
//#include "kim.h"
#include <string.h>
[code]....
View 4 Replies
View Related
Jul 20, 2014
I can't seem to get my program to read a file that's saved on my computer. Here's my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inputfile;
string name;
[Code] ....
View 6 Replies
View Related
Dec 10, 2013
int main() {
double x, avg, max=0.0, min=100.0,totalsat=0.0, totalunsat=0.0, totalout=0.0;
int invalid=0, outstanding=0, satisfactory=0, unsatisfactory=0, kount=0;
ifstream inFile;
inFile.open("grades.txt");
[Code] ....
When the program tries to open the text file it just scrolls "0 unsatisfactory score" forever. The text file has grades in it I'm trying to sort
63
75
121
72
72
78
67
-9
80 .....
View 1 Replies
View Related
May 19, 2014
How to write the code for this with the following requirements:
download the text file weblog.txt
This file is an Apache web log taken from the web server for St. Mary's University. When a visitor goes to their web site, the visitor's browser makes a request to the web server to view a web page, which is a file residing on the server. Each time a page is requested by a browser, the web server records information about that request. This weblog.txt holds the details of some of those requests.
Create a non-member function to read each line of the web log file. This function must do error checking to ensure that the file is opened successfully, otherwise it must provide a message like "file not available" to the user.
Each line should then be stored in a vector such that the first element of the vector is the first line of the web log file. Because each element will hold an entire line from the file, the vector should be declared as a vector of strings.
Note: You must declare the vector in a function.
Create another non-member function to sort the contents of the vector. Make sure to pass the vector by reference or your sort will disappear when the function ends! Use the sort function with #include <algorithm> to do the sort; you do not have to write your own sort algorithm.
Create one more non-member function to write the contents of the vector to a file. Each element should be on its own line in the new file. The contents of the new file should look like the original input file once your program completes, but in sorted order.
Create a main function that calls all of your non-member functions.
View 2 Replies
View Related
Jun 5, 2014
why I'm giving "Access violation reading location 0x336827B8" and also I was able to read my data but it's giving me weird stuff. I want to write the sorted grades and the average in a new disk file. so here's my code so far here's my code
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int avg(int sum, int size);
void swap(int *, int *);
[code]....
View 5 Replies
View Related
Dec 30, 2014
I would like to subtract one image from another to get something like "differential picture" (i am not sure whether the name is correct). I've managed to read the FILEHEADER and FILEINFOHEADER of bitmap. I will subtract 24-bit bmp files only. However i can't understand the structure of this file. So far my code looks like this. Any example of at least copying one bmp file to a new one? This example would be useful, because subtracting will be simillar, instead of copying the pixel value i will just put abs(value1-value2) in the output.
Code:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
struct BITMAPfileHEADER {
[Code] .....
View 4 Replies
View Related
Apr 14, 2014
Windows 7, 64 bits, Visual Studio 10.
I have a problem to read a large number of binary files, process them and store them under a new name. The program and routines go very well for 505 files. After reading 506 files, the program now refuses to read the next file. I have 16 Gb of memory and tried to close all other programs and restart the PC. it always stops after 506 files (512 files would be more understanding in a way...).
Here is my code. I have tried many things without success. This is only part of the loop that stops. The if test if (myfile.is_open() returns false by some reason. I can start the process again starting with the file that does not open and then it stops again after 506 files.
char * tfiBlock;
ifstream myfile (OrigFilename, ios::in|ios::binary|ios::ate);
if (myfile.is_open()) {
int lengde = myfile.tellg();
tfiBlock = new char [lengde];
//static char memblock [size];
[Code] .....
Clean up procedure:
delete[] tfiBlock;
Are there any limits to how many files that can be opened, or is it maybe someting to be set in the compiler?
View 5 Replies
View Related
May 31, 2013
I want to know how to read multiple text files in this program... I have 5 text files(order1,order2,...order5).
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<windows.h>
using namespace std;
class{
[Code] ....
View 3 Replies
View Related
Jun 9, 2013
I know it sounds strange but I've seen things that have files which contain source code (usually in something in Python or such) and how this is read on run-time?
View 1 Replies
View Related
Jul 7, 2013
I am using the code below to write a single instance of object "Employee" to a file in Binary mode. The write part seems to work fine, however when I try to read the single employee object from the file into memory I get a double free or corruption error.
I think this has to do with the fact that I am using a string data member in the Employee class but I don't understand what is going wrong. I have read that strings can vary in length and use dynamic memory allocation but if I write a single employee object to a file with data member 'name' equal to "John", it should be the exact same size when I read it back in right?
The code below works with no issues when I omit the string data member. Why is that? Where is the memory for the string object being "double released" when I read the employee object back into memory from the file?
I am using Linux Mint 15, Eclipse June and GCC 4.7.3 with the -std=c++11 option.
#include <iostream>
#include <fstream>
#include <string>
[Code].....
View 3 Replies
View Related
Oct 15, 2014
I'm in a CIS 143 class which is Introduction to Programming using C++. We've been assigned a lab with little to no detail on how to do the things listed.
The purpose of this lab is to practice the file input and output concepts. For this lab you may assume that the input files exist and contain appropriate data inside of them.
Four separate text files* which contain the following lines, different for each file.
(*: This isn't on the lab, but don't worry about what the name, age, id, and major are. those can be random and I can just change them what they need to be)
Name
Age
Student ID Number
Major
Your program should ask the user which of the four input files they would like to open, and then read the contents from the file. Once the contents have been read in, it should output to both the screen and a file named "output.txt" the four values were read in, arranged as follows:
"Hello <name>! You are a <age> years old <major> student, and your ID number is <ID number>"
Now I am by no means saying "Do it for me". I just need to know how to have a user type file1, file2, file3, or file4, and then have the program open that file, then have the output.txt file arrange the wording into the sentence above.
This is what I have so far and I don't think it is anywhere near correct:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream inFile;
string file1;
string file2;
[code]....
View 1 Replies
View Related
Nov 28, 2013
I'm trying to read stream data from a stream in a pdf file compressed in LZW. My first step is to read this compressed data into a buffer. The second step is to decompress it. I know how to decompress it, it is the first step I am having trouble with.
How should I read this data? If I read each character into a char the numerical output will not exceed 256. LZW compressed output should exceed 256.
I read the pdf in binary mode and have tried the read function like below.
From what I understand (don't quote me), I should be reading in 12 bits at a time. Is that correct and if so should I be using a bitfield. If I do need a bitfield then how to I read the data from the stream into a bitfield of 12 bits without restricting the binary value of the characters being read from the compressed stream.
Code:
ifstream inputfilestream;
inputfilestream.open("myfile.pdf", ios::out | ios::binary);
char mychar; //unsigned char gives an error though it can be //converted to unsigned char later.
while (inputfilestream.read(mychar, 1)) {
// do something with the char.. I have stored it in an int vector
}
View 14 Replies
View Related
Feb 5, 2014
I'm writing a code obfuscator in C. Debugger shows no errors in the code, but the program crashes after compiling -- I'm guessing it has something to do with while loops or reading data from files.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// list off all replaced elements
typedef struct ReplaceList {
char *from;// from string
char *to;// to string (random)
[Code] ....
View 3 Replies
View Related