C++ :: Reading File Twice In A Program?
Feb 13, 2015how to read file twice in a single program?
View 4 Replieshow to read file twice in a single program?
View 4 RepliesI have a program that makes a .txt file and I am trying to read it and it won't.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string fershizzle;
[Code] .....
I'm working with parallel arrays, one handles the titles and the other handles the ratings. Now everything works fine if the title doesn't start with a number, but if I decide to use a number infront of the title, when it reads it from the .txt file it thinks it is part of the rating.
Here is what I mean:
1234567891011121314151617181920212223242526272829303132333435363738394041424344
LOADING DATA FROM FILE...
MENU
1. Add Movie
2. Print All
3. Exit
------------------------------------------------------
Enter 1-3 : 2
PRINT ALL
------------------------------------------------------
Hitch 98% FRESH
Happy Gilmore 99% FRESH
Transformers 2 100% FRESH
SpongeBob 10% ROTTEN
MENU
[code]....
As you can see, the '2' from 2 Fast 2 Furious went to the rating in SpongeBob.
Here is what it looks like inside the .txt file:
Hitch| 98Happy Gilmore| 99Transformers 2| 100SpongeBob| 102 Fast 2 Furious| 100
Here is my code:
void SaveToFile(const string titles[], const int ratings[], int count)
{
string filename;
ofstream outfile;
filename = "tomatoes.txt";
outfile.open(filename);
PrintToText(outfile, titles, ratings, count);
WriteLine(cout, '=', 54);
[code]....
I'm not sure why my program isn't reading data from the dat file. I'm not sure if it's because I messed up my pointers, or I'm missing something. I know we usually use Infile >> blah >> blah >> blah; to get the data from the file, but my instructor showed an example that had the struct read it for you in this format and I could just call it with pointers.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <assert.h>
using namespace std;
using namespace System;
typedef int Integer;
[Code] ....
I need to decrypt my text file and make it able to read in my program..
What I have programmed so far is to read the encrypted file, create a new file, decrypt it and read the newly created file..
I need to decrypt the encrypted file without having to create a new file that reads the decrypted text..
Well, Let me show you my code:
P.S Most of the include is not needed and I already know that Visual studio 2010 Windows Form Application CLR
Code:
#pragma once
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <conio.h>
[Code].....
You have my decryption code and the code I want to use..
I have uploaded the code for you to use on your computer because it is fairly hard for me to explain..
I want to be able to read the encrypted file in my program, without having to writing a new file to decrypt it..
Decrypted & Encrypted .txt file Included (And images)
--> Program .rar pack link <--
Build it with Visual Studio 2010
We are supposed to split this lab into 3 files a .h and 2 .cpp's. The code I'll be talking about is going to be directed at the main.cpp file. Basically we are supposed to be able to enter as many classes into the program and when you are done the program is supposed to save the contents into a .dat file. This lab is working off of an older lab so I already have a .dat file I am trying to test with this program by reading it into my program when I first run it I prompt if the user wants to load previously entered courses and if they reply with a 'Y' the program will load the contents of the .dat file. Problem is right now when I load from the .dat file only the first class is correct. Every other class after that is just giberish. I've tried everything but can't get the restore part working. I also cannot get the save to .dat portion of this lab working either.Here is my code:
#include "Course.h"
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
[code]....
Why program crashes when reading linked List from file?
#include <iostream>
#include <fstream>
using namespace std;
struct link{
int data;
link* next;
[Code] .....
I have written an program to emulate a shopping list. Everything works fine, until I try to read in from a file. Here is my read and write code (the write code looks like it is working ok, but I figured I would include it just in case), and an example of what happens to a list that has been written, then read back in.
I took this list, and wrote it to a file, then read that file back in. The .txt file looks like everything was written out properly, but when I try to read that exact same list back in, this is what I get:
Program background: Ticket Sales Details You will sell tickets in advance and at the door. Prices for buying in advance and at the door will be given. Also, the total number of tickets sold in advance will be given. Each guest will have a unique number. If there are n tickets sold in advance, then these guests will be numbered 0 through n-1. As the event starts, requests to buy tickets at the door may be made and these guests will be numbered sequentially, starting at the lowest unassigned number. The maximum number of guests will be 1000. The first line of the file contains the following three values, separated by spaces: Cost of the presales tickets (in dollars), Cost of the tickets at the door (in dollars), and the number of presale tickets. The first two values will be positive real numbers to two decimal places and the last will be a positive integer.
The second line of the file will contain one positive integer representing the number of auction items followed by a positive real value to two decimal places (at most) representing the minimum bid increment, in dollars. The first value is guaranteed to be 1000 or less and the second will be in between 1 and 50, inclusive.
The third line of the file will contain all the prices of the auction items, in dollars, separated by spaces, in order. Thus, the first price is the price of item 0, the next price is the price of item 1, and so on. These values will be real numbers represented to up to 2 decimal places.
The fourth line of the file will contain the three following positive integers pertaining to the raffle: the number of raffle tickets available, the cost of a raffle ticket in dollars, and the number of raffle prizes. (It's strange to have raffle tickets that don't cost a whole number of dollars.)
The fifth line of the file will contain each of the values of the raffle items, in dollars, separated by spaces, in order. Thus, the first price is the price if item 0, the next price is the price of item 1, and so on. These values will be real numbers with upto 2 decimal places.
The sixth line of the file will contain ten positive integers representing the number of each of the drinks 0 through 9, in order, that are in stock.
The seventh line of the file will contain ten positive real numbers with upto 2 decimal places representing the price of each of the drinks 0 through 9, in order.
The eighth line of the file will contain a single positive integer, numEvents, representing the number of events that occur at the charity ball. These events are split into two groups: actions by guests at the ball and awards given (raffle, auction, person, totalrevenue). All of the actions precede all of the awards. You will produce exactly one line of output for each event described. Here are the formats of each event that could occur:
If a patron buys a ticket at the door, a command will be on a line by itself:
BUY TICKET k
where k is a positive integer indicating the number of tickets bought at the door. These guests will be numbered as previously mentioned. You are guaranteed that the total number of tickets bought, including presales, will not exceed 1000. This is what I have so far and I cannot figure out why it wont calculate the total revenue. I am not completely sure if it is even accessing the if statement in main.
Code:
#include <stdio.h> #include <stdlib.h>
#include <string.h>
#define N 1000
[Code].....
I just took up c programming and like it so far. Now I got a problem reading in a character in a program. The program looks like this:
insert
Code:
#include <stdio.h>
main()
{
char c1, c2;
printf("Letter 1: ");
c1 = getchar();
[Code]....
I am reading a file then printing the data onto the other file. It is working, however when I check to see if each variable is being properly set after reading the file a issue arises.
Example of the file being read
Code:
Vehicle PV50CAN passed camera 1 at 05:33:26.
Vehicle W867BRO passed camera 1 at 05:33:29.
Vehicle KQ63ARU passed camera 1 at 05:33:38.
Vehicle K954ITQ passed camera 1 at 05:33:40.
Vehicle V220MXB passed camera 1 at 05:33:42.
[Code] .....
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]....
I'm using scanf() function to read numbers in my cpp program and, for example, if i want to read a string that is a complete name i could use
scanf("%[^]", x);
or if i want to read only vogals i could use
scanf("%[aeiou]", x);
Is it possible to read only positive numbers or only negatives numbers using scanf() and how to do it?
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] ....
This is the problem :- Write a program that keeps reading integers until user enters -1, then it prints the maximum and the minimum among all numbers (-1 should be ignored).
View 14 Replies View RelatedI'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] ....
I am trying to write a program that will open a file with 2 points, take those points, determine what quadrant those points are in, and save the result back into the same file without truncating the data.
I'm having some trouble with my file I/O. I can't get my program to read the two values for X and Y that I put in the points.txt file. It simply uses whatever random number was used last time in that block of memory. In my points.txt file I put something like "4 -5" and it wont assign those numbers to my X and Y variables. I believe the issue is on line 25
#include <iostream>
#include <fstream>
using namespace std ;
int main() {
//defining X and Y
double X, Y ;
[Code] ....
If I am reading data from a .txt file with a person's last name and first name (e.g. Miller Andrew), How do I output the same data with the first name followed by the last ?(e.g. Andrew Miller) Do I use the ignore function?
View 2 Replies View RelatedI want to know how to read a .pgm file from my hdd and store it in a 2d array so i can perform some action on the array later on. This is ny first time working with an imqge in c so i know nothing about it. .
View 3 Replies View RelatedI have been given a task to make a c++ program that promptsd the user to enter a mp3 file name, read the ID3v1.1 tag and displays it. It then should prompt the user to to decide if they want to edit the comments section of it, then if they edit the section, it should re-print the updated ID3 tag. The list of genres are in a cstring that is as follows.
char genre[126][128] =
{
"Blues" "Classic Rock", "Country", "Dance", "Disco", "Funk",
"Grunge", "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other",
"Pop", "R&B", "Rap", "Raggae", "Rock", "Techno", "Industrial",
"Alternative", "Ska", "Death Metal", "Pranks", "Soundtrack",
[Code] .....
Using the feof() function in the output file i have the last value twice.
This is the input:
1 3 6
1 1 4
0 3 1
1 1 6
0 6 2
1 1 6
And the output should be:
12
6
15
13
But using the feof() the last value appears twice:
12
6
15
13
13
How can i change that?
while(!feof(pfile1)) {
sum=0;
fscanf(pfile1,"%d%d%d",&choice,&day,&val);
if(choice==0)
[Code] ....
How to read a grayscale jpeg image in C++ ?
How do I write the code to read the jpeg file and store its content in a 2D array
I am practicing with C++ since I done most of C already and I have a problem with reading spaces from a .txt file.
I created a little program that creates a wannabe Cube in wannabe 3D xd, well to appear as 3D like:
Code:
cout << " O------O" << endl;
cout << " / /|" << endl;
cout << " / / |" << endl;
cout << " / / |" << endl;
[Code] .....
The actual code is a bit longer since it offers you to input the size and then it draws the pic. Now that wasn't so hard and I've done that but now I wanted to implement the "MessageBox" func for output.
I managed to write the cube in file cube.txt but when I'm reading from it 1 char at a time since I need to output as char array it avoids all spaces and new lines and just puts all symbols in the same row.
I didn't have that issue with C and I've found on stackoverlow a solution using strings & getline but I need it to be in "char" form.
How to actually read spaces and newlines? This is my current code for reading from file:
Code:
ifstream di("kocka.txt", ios_base::in);
char c[5000];
int br=0;
while( di >> c[br])
{
br++;
}
MessageBox(NULL, c, "Kocka", MB_ICONHAND);
di.close(); P.S kocka = cube (in croatian )
I'm writing a program using Huffman algorithm to compress text file. I have tested my program by just printing the printing ASCII character to file and it worked fine. However, now I have to implement using bits and my program doesn't work. It seems like I'm not reading or writing the right bits. Here is the result of my testing:In the input file I put abc the input file to compress it. Then I uncompress it the out out is aaa. Below is a snippet of how I read and write bits
Code:
class BitInput {
istream& in; // the istream to delegate to
char buf; // the buffer of bits
int nbits; public:
BitInputStream(istream& s) : in(s), buf(0), bufi(8) { }
[Code] ....
Basically I want to open a binary file containing utf-8 encoded characers. The length of the encoding is variable: 1, 2 or 3 bytes.
Rules are:
if the read byte has 0 as strong bit then it represents a single byte.
if the read byte has 11 as strong bits then it represents the first byte of a 2 or 3 length character.
if the read byte has 10 as strong bits then it represents the second or third byte of a 2 or 3 length character.
Each character encoded on 2 or 3 byte has to start with a 11xxxxxx and following bytes has to start with 10xxxxxx
So i need to go through his file, read byte per byte to analyse what kind of byte it is, sometimes go back to previous character etc etc
My goal is to identify all encoded characters in the file and then process them by convering them to Unicode value, storage in array etc...
So im just trying to read in an pgm file and store it in an array,
Code:
int** imageArray;
//allocate the image array
imageArray = (int**) malloc(row * sizeof(int*));
for(i = 0; i < row; i++)
imageArray[i] = (int*) malloc(col * sizeof(int));
[Code]....
It stores about 1/3 of the file into the array, and then starts printing 6815940 6819608 followed by a bunch of 0's.