C/C++ :: Reading File And Printing Stuff
Jun 5, 2014
void readpatient() {
int count = 0;
f = fopen("patient.txt","r");
while(!feof(f)) {
printf("
%d",count++);
p2=new patient();
[Code] .....
Here is what I've tried. I'm trying to read a file aND print stuff in it, but my while loop isn't ending up.. What should i do? It only prints patient name 1 time and after that it prints nothing.
removing system("pause");
results in an endless loop.
View 2 Replies
ADVERTISEMENT
Feb 26, 2015
I need a program that reads the number of lines of a file and then several (max of 20) lines of text from a .txt file so an example of the .txt file is: 2 This is the first string This is string number 2 So it first reads the 2 and then reads the two lines of text. It stores the text in an array of pointers. The code i have so far is:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
[Code].....
It doesnt give me any errors but all it does is keep running and doesnt print anything out kind of like its in an infinite loop although i dont see how that could be possible.
View 1 Replies
View Related
Dec 1, 2013
I am trying to read hex data from a file (not just hex values in text format). I was able to read it (02 EC) with "fread" into an char before but I need to change the Hex value into an integer in decimal.
I already read about strtol but I would prefer reading the hex Value into an integer.
Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int itrack1[1];
int itrack2[1];
[Code] ....
View 11 Replies
View Related
Nov 7, 2014
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] .....
View 14 Replies
View Related
Apr 10, 2015
I have a program where I need to read in a 96 number .txt file and perform some operations on it.However, the numbers it reads in are all inncorrect. I got it to work in XCode, but it doesnt work in DevC++ which is where the teacher will be running it. Do I have any major error?
#include <stdio.h>
#include <stdlib.h>
#define rows 8
#define cols 12
void makeArray(FILE*,int[][cols]);
int getScore(int[rows][cols],int,int);
[Code]......
View 14 Replies
View Related
Mar 8, 2013
I'm writing a program that reads from stdin and prints it back out. I'm getting a bunch of garbage then a seg fault.
Code:
double numbers[256];
memset (numbers, 0, sizeof (numbers));
for (;;) {
double num = scanf ("%255lg", numbers);
if (num== EOF)
break;
}
for (int i = 0; i < (int)sizeof (numbers); ++i) {
printf ("%g
", numbers[i]);
}
View 3 Replies
View Related
Feb 20, 2013
I got as far as getting a web page into an IHTMLDocument2 but I don't know what to do from there, all the examples I found are C# or .NET or something else I don't understand.
Any simple example in C++ of getting all the links from an IHTMLDocument2 ?
I was able to do this:
Code:
IHTMLElementCollection* collection;
hResult=document->get_links(&collection);
long nLinks;
collection->get_length(&nLinks); // returns correct number
collection->Release();
How do I loop through the collection and extract the actual links ? Also, if they come out as BSTR do I simple treat them as WCHAR* ? If I can do that I can figure out the rest myself.
View 2 Replies
View Related
Feb 23, 2015
I have been skimming and searching but dont know how to word the search just right to find what I need to know. I have written simple stuff with the support of tutorials like weight conversion or loops counting up to a set number. Nothing amazing. The other day I found out how to input and print a string rather than a single text character which i though was bad ass. I even got it to read multiple strings on a single line and found a way to read multiple lines. I can even format it to read both integers and characters on the same line as long as I know the predefined format.
On to my question... How do I read multiple lines with both carecters and integers. for instance:
nissan 1996
toyota 1998
or more comples like
nissan gtr 1996
toyota markii 1998
I want to use
int year;
char make[10]; maybe need to use char make[10][10]; for an array i would guess.
char model[10]; optional for the extra data
but reproduce what i read in a different order. say...
1996 nissan
1998 toyota
vice the original format.
this is what I have tried.
Code: scanf("%s %s", &make,&year);
//The way I seen to read multiple lines was on here
scanf("%[^/t]", %make);
But this wont let me separate the two into two differnet definded data types. Let alone use printf to display them in reverse order.
View 1 Replies
View Related
Jan 16, 2012
We are in process to get rid off Roguewave library while moving from AIX to Red hat Linux.
Need to replace RWZone, RWTime stuff with standard gnu implementation on RHL.
View 2 Replies
View Related
Apr 16, 2014
Using cin.sync() works great so far, but my program still breaks if you copy something with multiple lines into the console.
string test = "";
while(true) {
cin.sync();
getline(cin, test );
cout << endl << "test: " << test << endl;
}
However, if you were to copy this:
1
2
3
and paste it into the program, the output would be1
2
3
test: 1
test: 2
And if you press enter one more time:1
2
3
test: 1
test: 2
test: 3
The 3 finally pops out.
View 2 Replies
View Related
Feb 12, 2013
I'm writing a program that stores records into a file and then these records can be printed out. A last name, first name, and score is stored to be exactly 36 characters long (using leading spaces to pad) making it easier to retrieve specific records. For example, three records stored in the file would like like this: (the underscores are simply to illustrate the distance, they are not in the file itself)
_______lastname_______firstname__90__________lname __________fname_100___________last___________first __60
When printed out, the names are formatted as follows:
lastname, firstname: 90
lname, fname: 100
last, first: 60
However, when I print them out this is what I get:
lastname, firstname: 90
lname, fname: 100$
last, first: 60H
For some reason, for any record after the first, an extra character is added to the end. These characters are not in the file, so I was thinking that the array for some reason wasn't being filled completely, (the array is initialized to size 36 and 36 characters are read from the file using fread) so it was printing out a random character assigned to the 36th array position. Except the character never changes, (always a $ for record 2, H for record 3, l for record 4 if i remember) and I've tried reducing the array size or the number of character read and it's the string that gets altered, the random character always remains. I figure the problem must be in the print_records function (appending seems to work no problem). Anyway here is my print records and appending records code.
Code: /*
- Prints a single record stored in the file pointed to by ifp.
*/
void print_record(FILE *ifp, int record) {
[Code]......
View 6 Replies
View Related
Feb 8, 2014
I need c source that give the url txt file and print the txt file
(get the http://site.com/text.txt) and print the (text.txt) file Content.
View 1 Replies
View Related
Sep 13, 2013
The requirement is to capture statistics of uuid occurrences for ALIVE/SEARCH/BYEBYE (it can be all 3, combinations of 2 each, or one alone) in a dynamically populated file in run time.
I am able to print all 3 combinations, but not in combination of 1 or 2 e.g.
If my input.txt is like this :
uuid:22314754-a597-490b-8a93-02cfae01036b ALIVE 16
uuid:22314754-a597-490b-8a93-02cfae01036b BYEBYE 8
uuid:22314754-a597-490b-8a93-02cfae01036b SEARCH 8
uuid:50e65653-7525-485d-83bf-d293558c4264 ALIVE 32
uuid:50e65653-7525-485d-83bf-d293558c4264 BYEBYE 8
uuid:50e65653-7525-485d-83bf-d293558c4264 SEARCH 132
[Code]...
Code:
#include<stdio.h>
#include<string.h>
struct uid
{
char uid_val[100];
char state[100];
int temp_count;
int alive_count;
int search_count;
[Code]...
Gives the following output ->output.txt)
Device ID ALIVE BYEBYE SEARCH
uuid:22314754-a597-490b-8a93-02cfae01036b 16 8 8
uuid:50e65653-7525-485d-83bf-d293558c4264 32 8 132
uuid:55076f6e-6b79-4d65-6497-180373763bc1 113 112 111
uuid:T0100203354 1 2 3
I want to generalize the code such that uuid occurrence does not have to be all 3 (ALIVE/SEARCH/BYEBYE), the occurrences can be any combination and code should work. e.g my code gives wrong results when input.txt contains the following:
uuid:22314754-a597-490b-8a93-02cfae01036b BYEBYE 8
uuid:22314754-a597-490b-8a93-02cfae01036b SEARCH 8
uuid:50e65653-7525-485d-83bf-d293558c4264 ALIVE 32
uuid:50e65653-7525-485d-83bf-d293558c4264 BYEBYE 8
uuid:55076f6e-6b79-4d65-6497-180373763bc1 ALIVE 113
uuid:55076f6e-6b79-4d65-6497-180373763bc1 BYEBYE 112
uuid:55076f6e-6b79-4d65-6497-180373763bc1 SEARCH 111
uuid:T0100203354 BYEBYE 2
I am using ubuntu for gcc/g+ compiler.
View 1 Replies
View Related
Aug 10, 2014
I have a file with data in it, and I'm trying to print that data, but it is:
1) not reading the last file (record #83)
2) it's printing out the last record twice (in this case, #77)
//record data for each tool
int recordNumber;
string toolName;
int quantity;
[Code]....
here is the printData() function
void Hardware::printData(int _recordNumber, string _toolName, int _quantity, double _cost)
{
cout << left << setw(10) << getRecordNumber() << setw(20) << getToolName()
<< setw(10) << getQuantity() << right << setw(10) << fixed << setprecision(2) << getCost() << endl;
}
View 9 Replies
View Related
Aug 24, 2013
these are the errors that I'm getting from an online c++ compiler
// main.cpp:4:10: error: #include expects "FILENAME" or
// main.cpp: In function 'void permute(char*, int)':
// main.cpp:17:9: error: 'f' was not declared in this scope
I don't understand how to print full_string to a file!// otherwise, I know that it gives the correct output -- 90 strings.
#include <string>
#include <iostream>
#include <fstream>
#include >ios> // line 4 error
using namespace std;
char full_string[] = "112233";
[code]....
//iter_swap – it just swaps the elements pointed to by the respective pointers without changing the pointers themselves. so, it's basically equivalent to the function:
void iter_swap(char *ptr1, char *ptr2) {
char tmp = *ptr1; *ptr1 = *ptr2;
*ptr2 = tmp;
}
// min_element – finds the location of the minimum element that exists in the given range. in this case,
it is the char* pointer pointing to the location of that element. it can be implemented like:
char *min_element(char *start, char *end) {
// end is 1 beyond the last valid element
if(start == end) return end; // empty range
char *min_pos = start;
for(char *iter = start+1; iter != end; ++iter)
[code]....
View 7 Replies
View Related
Sep 4, 2012
I am encoding some information in a binary file, and I want to check what I am doing by printing out all the bytes that represent the file.
This is being done by opening a pointer to the file with fopen, reading in each byte of data as a char, and then writing this char to the screen.
I have some image files (e.g. "image.jpg"), whose structure I know, so that I can test my program.
When I print out the chars, they are initially correct, and follow the structure of the file as expected.
However, after about 40 bytes, I find that every subsequent character is ' ' i.e. a blank character.
I then created a CharToBin function, which allows me to print out the actual bits in the char. When doing this, it shows that all the bits are 1 for the characters. i.e. most of the file is represented by 1's, which is clearly not correct.
This happens on all the image files I have tested, and furthermore, on several other non-image files. They all start printing out ' ' after a while. However, all these files are fine and not corrupted, e.g. the image files display correctly.
Code:
#include <fstream>
#include <iostream>
#include <sstring>
#include <string>
#include <stdio.h>
using namespace std;
string CharToBin(char ch) {
bool bits[8];
for (int i = 0; i < 8; i++)
[Code] ....
View 6 Replies
View Related
Jun 27, 2013
I have done an exercise from "Thinking in C++" to print text from a file to the console one line at a time, with the user pressing enter to get the next line.
My original code is below, how could I change it for one word at a time?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
[Code] .....
I tried to alter my program to print one word at a time by changing "getline(in, word)" to "in >> word", but it prints each new word on a new line, the only input I have found to make the next word appear is Enter, I assume that's why.
Is there a way to make it print one word at a time when the user says, but on the same line?
View 4 Replies
View Related
Nov 14, 2013
I need to create a text file that looks like:
first line
second line
third line
forth line
fifth line
sixth line
I want to replace the third and forth lines with three new lines. The above contents would become:
first line
second line
new line1
new line2
new line3
fifth line
sixth line
How can I do this using c++?
never was taught file function in c++
View 3 Replies
View Related
Oct 11, 2013
I have to convert a binary value from an input file (the name of which is given by the user) and then convert the binary to decimal value and print that in an output file.
Right now it is compiling just fine with no error messages, but when I run the program, it doesn't end or print to the output file.
Here is my code at the moment:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
[Code].....
View 2 Replies
View Related
Jan 14, 2014
I have 3 arrays (even, odd and negative), they are all different sizes, but they maximum elements they can have is 50. How do I write them to a file in this format
EVEN ODD NEGATIVE
2 3 -2
4 5 -4
6 7 -9
Code:
size = 50; //they dont all 50, its just that array cant be bigger than 50
for (int i =0; i < size; i++) {
cout <<setw(5);
if (even[i] != 0) {
myfile << eList[i];
myfile << endl;
[Code] .....
View 5 Replies
View Related
Dec 6, 2013
Following is the program I wrote it basically takes 9 inputs and then save them into binary file. then print out the data stored in binary data and find inverse of it then print the inverse out. but its stuck in a loop somewhere.
Code:
#include <stdio.h>
int main() {
int a[3][3],i,j;
float determinant=0;
int x;
FILE *fp = fopen ("file.bin", "wb");
[Code] .....
View 6 Replies
View Related
Jan 2, 2015
I am trying to establish a connection between client and server to send a file to the server from a client. I have been successfully able to send files to the server but i am facing a problem with the the filename whenever i try to send any string to the server and use it in naming the filename at the server side, the string is successfully concatenated but it saves the filecontents in the filename.
for example: i am sending a file hello1.txt to server and the server has to save it as abcxyz.txt as i am sending the "xyz" from the client. BUT Whenever i am doing this ,the file saves as abcxyzfilecontents.txt If i saved in the .txt file "you123" ,my file at server side would save as abcxyzyou123.txt
Here are my codes:
Know that the server code implements a multi threaded server. The functionality to be discussed is defined in myfunc
Code:
server.c:
#include <stdlib.h>
#include <stdio.h>
[Code].....
View 1 Replies
View Related
Mar 21, 2013
So I'm doing a kind of banking system. Atm I've got the user able to create an account and set the balance of it, which is written to a .txt file. However, I would like the user to be able to enter their account no. and the program to cout the account info on the screen. I will be adding more to the program later, but until I work out how to search for values in a .txt file, and print out values on that line I'm a bit stuck.
Code:
Main.cpp -
#include <iostream>
#include "create_account.h"
#include <fstream>
using namespace std;
int menuin;
int main() {
create_account creataccObj;
[Code] .....
Sample .txt file -
// Account_no - Balance //
1001 140500
1002 150600
1003 18600
View 8 Replies
View Related
Nov 26, 2013
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].....
View 4 Replies
View Related
Jan 28, 2014
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] ....
View 14 Replies
View Related
Jan 5, 2014
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 Related