C++ :: Printing Out Bytes Of A File

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


ADVERTISEMENT

C/C++ :: Accommodate Double-size 8 Bytes In 4 Bytes Pointer In 32bit System?

Mar 15, 2015

how to accommodate double-size:8 bytes in 4 bytes pointer in 32bit system.

View 1 Replies View Related

C++ :: Why Address Of Object Is 6 Bytes And Not 8 Bytes On 64 Bit Linux

Mar 24, 2013

Code:
int i12 = 1001;
cout << i12 << " " << &i12 << endl;

gives the result: 0x7fff0d065098

It's 6 bytes, but I'd expect the address to be 8 bytes on my 64 bit machine. I am using Ubuntu 12.04, GNU compiler.

So, why the address is 6 bytes and not 8 bytes?

View 10 Replies View Related

C :: Checking How Many Bytes Are Remaining In A File?

Feb 22, 2013

while bytesremaining >=8 do something;
if bytesremaing <8 then something else .

I am new and don't know how to code for this.

View 2 Replies View Related

C++ :: Deleting Certain Bytes From Binary File?

May 26, 2013

How to delete certain bytes from middle of a binary file?

Those bytes are in fact of a struct.

One approach I found was to re-write the whole data except the struct I want to delete. But may be its not good one if file size is in gb and I will have to write complete data again.

View 1 Replies View Related

C/C++ :: Reading Hexadecimal Bytes From A File?

Jan 19, 2014

#include <stdio.h>
#include <stdlib.h>
int main(int argc, int * argv) {
   char buffer[16];
   FILE *fp;
   unsigned long long test;
   unsigned char key[16];
   fp = fopen("D:key.txt","r");

[code].....

i searched for a code for reading bytes from a file and i found this one but when i run the program, it doesn't return the bytes written in the file, it returns another bytes / what is the problem?

View 3 Replies View Related

C/C++ :: Read Bytes From A Data File

Dec 5, 2014

I am a member of a group still developing the Microprose European Air War game. We have the source code and permission to do this. As the code dates back to 1998 I use Visual Studio 2006.

I have written many programs in Visual basic, and have the following one to read the airbase value in the "targets.dat" file. The file has a four byte header containing the number of tatgets, and a 32 byte data block containing the data for each target.

Private Sub Command1_Click()
List1.Clear
Dim nt As Long
Dim a4() As Long  
Open App.Path & " argets.dat" For Binary Access Read As 1
'read and display the number of targets  

[Code] ....

Despite many attempts I cannot write the C equivalent of this code into the eaw.exe source. All I really need to know is how to open the 'targets.dat' file and how to get the value of a byte (or integer, or long) at a given position as in the "Get #1, k, a4(n)" line.

View 1 Replies View Related

C/C++ :: Read A Line With 8 Bytes Of Data From A File

Jul 23, 2012

How to read a line with maximum of 8 bytes from a line in a file.

Let say line has 10248 bytes in one single line and same repeats for remaining lines in the file.

I need to read the first 8 bytes of a line and go on read the last 2 bytes of the line and repeat the till end of file.

View 3 Replies View Related

C/C++ :: How To Write 0 Or 1 To Binary File In Bits Not In Bytes

Aug 20, 2014

How I write a 0s or 1s to binary file in bits not in bytes how can i do that ???

View 2 Replies View Related

C Sharp :: Split A File In Equal Size According To Its Bytes

Apr 10, 2013

how to split a file in equal size and when clicking on split button it split the files as well as encrypt split parts and the size information are automatically stored in groupbox and save all splitted files in folder.

View 1 Replies View Related

C :: Printing Contents Of A File - Prints One Extra Character Not Present In The File

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

C++ :: Reading File Then Printing Data Onto Other File

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

C :: Printing TXT File From URL

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

C :: Printing Of A File In Different Format?

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

C/C++ :: File Printing Out Data Twice?

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

C++ :: Printing A String To A File?

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

C++ ::  Printing From File One Word At A Time

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

C++ :: Printing From Open Text File?

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

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 View Related

C++ :: Printing Function Output To A File?

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

C/C++ :: Reading Strings From A Text File And Printing Them

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

C/C++ :: Reading Hex Values From File And Printing Them In Decimal

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

Visual C++ :: Printing Arrays To File In Order

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

C :: Binary File Write By User Input Then Printing Binary File Data Out

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

C :: File Contents Printing And Saving With Filename Whenever Try To Send

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

C++ :: Banking System - Searching For And Printing Values From TXT File

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







Copyrights 2005-15 www.BigResource.com, All rights reserved