473,386 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

To output the contents of a text file

18
Hye,

I wanted to get a help to display the contents of a text file.
It contains 1024 bits but output shows just 300 bits.
There may be some problem. The program should start reading the file from beginning, but its counting only last 300 bits.

Here's my code :
Expand|Select|Wrap|Line Numbers
  1. #include "iostream.h"
  2. #include "stdafx.h"
  3. #include "conio.h"
  4. #include "string.h"
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8.  
  9.      char ch;
  10.     char fname[]="\0";
  11.     strcpy(fname,"D:\code.txt");
  12.     FILE *fp;
  13.     fp = fopen(fname,"r");
  14.  
  15.     while(!(fread(&ch,1,1,fp)==0))
  16.     {
  17.         cout<<ch;    
  18.     }    
  19.  
  20.     fclose(fp); 
  21.     getch();
  22.     return 0;
  23. }
  24.  
Any one can help..!
Thanks.
Mar 5 '08 #1
4 1773
gpraghuram
1,275 Expert 1GB
Hye,

I wanted to get a help to display the contents of a text file.
It contains 1024 bits but output shows just 300 bits.
There may be some problem. The program should start reading the file from beginning, but its counting only last 300 bits.

Here's my code :
Expand|Select|Wrap|Line Numbers
  1. #include "iostream.h"
  2. #include "stdafx.h"
  3. #include "conio.h"
  4. #include "string.h"
  5.  
  6. int main(int argc, char* argv[])
  7. {
  8.  
  9.      char ch;
  10.     char fname[]="\0";
  11.     strcpy(fname,"D:\code.txt");
  12.     FILE *fp;
  13.     fp = fopen(fname,"r");
  14.  
  15.     while(!(fread(&ch,1,1,fp)==0))
  16.     {
  17.         cout<<ch;    
  18.     }    
  19.  
  20.     fclose(fp); 
  21.     getch();
  22.     return 0;
  23. }
  24.  
Any one can help..!
Thanks.

Is this a C or C++ code?
Also what you mean by 1024 bits..
In a file every character is a byte.
Can you attach the ip file also

Raghuram
Mar 5 '08 #2
jhamb
18
Is this a C or C++ code?
Also what you mean by 1024 bits..
In a file every character is a byte.
Can you attach the ip file also

Raghuram
Its a CPP file..
Code.txt file contains digits 1 or -1.
Here we use -1 in place of 0.
They are 1024 in total.
Attached Files
File Type: txt code.txt (5.0 KB, 376 views)
Mar 5 '08 #3
In a txt file the minus sign is a character.so when you say 1024 -1's and zeros you need to account for those. also depending upon the whay its written your loop to display the code may count a line break as a character.
So taking this into account
300 x 1
150 x -
300 x \n newline
300 x \r carriage return

1050 - give or take a few...

this is why you are only getting about 300 of your characters.
because each character is treated individualy. Hope this helps....
Mar 5 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
If this is C++ code, then you should be using <iostream> and not <iostream.h>.

iostream.h is the pre-ANS C++ that was used prior to 1998 so it's 10 years out of date.

Also, the C-string library is deprecated in C++. You should be using the C++ string object and not C-strings that are kept in arrays.

Also, you should be using file stream objects and not eh C FILE* stuff.
Mar 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Row | last post by:
Hi, I am having trouble writting from a JTextArea to a flat text file (locally) I have tried using string tokenizer to tokenize the string first but with no luck, Im stuck on the one :( im...
1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
1
by: nomadx | last post by:
hello, could someone please check this out? #usr/local/bin/perl # Filelist.pl version 0.1 use strict; use CGI qw(:all); use CGI::Pretty; #use CGI::Carp qw(fatalsToBrowser); use Cwd; my...
4
by: Mike Conmackie | last post by:
Hi Folks, I've probably omitted something very basic but I have no idea what it might be. The results of my transformation _should_ be an xml file but all I get is the xml declaration...
6
by: Wescotte | last post by:
I'm writing a tiny php app that will log into our bank of america account and retrieve a file containing a list of checks that cleared the previous day. The problem I'm running into is when I...
1
by: Aalok | last post by:
This is what i want to do. Read a text file as an input and based on that file, Create an outpu text file which saves the contents of the input file in a specifi format in the output file. I...
1
by: TJ | last post by:
I am very new to C# (this is my first real project), therefore please be patient if my question is considered being to newbie. I am modifying a program which takes a text file, does some...
0
by: mfilpot | last post by:
I am working on a prime number generator for sschool, but I jsut can't seem to figure out how to write the contents of the variable MeSSage into a new text file, Can someone help me? the script...
0
by: 14Dallas | last post by:
Hi, I have been working with another programmer to write this code. I haven't written code in years - DBase III and Pascal - anyways, back to my code question. The program opens a file and...
4
by: Jon Slaughter | last post by:
I'm using eval to excute some mixed php and html code but I cannot debug it. I am essentially using filegetcontents to load up a php/html file and then inserting it into another php/html file and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.