473,405 Members | 2,141 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,405 software developers and data experts.

reading a file that doesn't exist

Hi,

I have got a small program that prints the contents of files using the system call - read.

Expand|Select|Wrap|Line Numbers
  1.     unsigned char buffer[8];
  2.     size_t offset=0;
  3.     size_t bytes_read;
  4.  
  5.     int i;
  6.  
  7.     int fd = open(argv[1], O_RDONLY);
  8.  
  9.     do{
  10.         bytes_read = read(fd, buffer, sizeof(buffer));
  11.         printf("0x%06x : ", offset);
  12.  
  13.         for(i=0; i<bytes_read; ++i)
  14.         {
  15.             printf("%c ", buffer[i]);
  16.         }
  17.         printf("\n");
  18.         offset = offset + bytes_read;
  19.     }while(bytes_read == sizeof(buffer));
  20.  
Now while running I give a file name that doesn't exist.
It prints some kind of data mixed with environment variables and a segmentation fault at the end.

How is this possible? What is the program printing?
Feb 19 '11 #1
1 1577
horace1
1,510 Expert 1GB
you should check that the file opened OK
Expand|Select|Wrap|Line Numbers
  1.    int fd = open(argv[1], O_RDONLY);
otherwise when you attempt to read you get rubblish or segmentation faults, see
http://www.cprogramming.com/tutorial/cfileio.html
Feb 19 '11 #2

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

Similar topics

0
by: vikashskumar | last post by:
Hello everone, I am stuck in determining whether a file does not exist or does not have enough permissions so that access to this file is denied?". I am using java.io.File.exists() or...
9
by: ben | last post by:
Hello, This really drives me nuts. Im opening an input file, and Im testing if it was open successfully. Thats it! I know that the file that Im trying to open DOES NOT exist but Im getting NO...
11
by: Matt DeFoor | last post by:
I have some log files that I'm working with that look like this: 1000000000 3456 1234 1000000001 3456 1235 1000020002 3456 1223 1000203044 3456 986 etc. I'm trying to read the file...
4
by: Rush | last post by:
If i create a new StreamReader object, based on a filename that does not exist, the SR correctly raises an exception that the file doesn't exist. Great! I love it. BUT....then SR proceeds to...
3
by: Dave Moore | last post by:
Hi All, Ok, here's my problem. I want to open a file and process its contents. However, because it is possible that the file may not exist, I also want to check whether the file() function is...
1
by: UJ | last post by:
What do you do if the configuration file for you configuration block doesn't exist? There doesn't seem to be an easy way to create the file. I could just create the file but that implies I know the...
4
by: norm4h8 | last post by:
Hi everyone! I have a problem with trying to open a file in C. The following line in my code is suppoed to open a specified file if it exists and create a new one with this name if one doesn't...
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
2
by: kilo | last post by:
Hey.. I need someone hwo can help me making my sql table.. I have no php skills. I have payed for a php program that shoud make dictation for people that have some problems reading danish.. with...
2
by: Perl Beginner | last post by:
Hello all, I am new at VB6, but the more i learn, the more i love it! But i do have a question. I've created a form, the user has to choose an excel file from a list that they want to open, when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.