Opening a File in "C"


 
Thread Tools Search this Thread
Top Forums Programming Opening a File in "C"
# 1  
Old 11-06-2010
Opening a File in "C"

Hello

I am finishing an intro to C programming, the next to last chapter is giving me a problem here is my code:
Code:
#include <stdio.h>

int main (int argc, const char * argv[]) {
	FILE	*fp;
	int	c;
	
	fp = fopen( "../Five", "r" );
	
	if ( NULL == fp ) {
        printf( "Error opening ../My Data File" );
    } else {
		while ( (c = fgetc( fp )) != EOF )
			putchar( c );
		
		fclose( fp );
	}
	
	return 0;
}

I have also tried, ~/Five , and ../..Five . The file is located on my hard drive. What is wrong?Smilie

Last edited by Scott; 11-07-2010 at 06:19 AM.. Reason: Please use code tags
# 2  
Old 11-07-2010
Not sure what your problem is. I cut/pasted your code and it works without a problem. Are you sure that the file Five is in the parent directory when you execute the binary?

It can also be helpful to print the error associated with the failure if the file cannot be opened:

Code:
#include <errno.h>
:
:
printf( "error opening input: %s\n", strerror( errno ) );

# 3  
Old 11-07-2010
well..

it posts the same error, "error opening ../mydatafile" i am using xcode for mac i do not know too much about commands to call the files, the file titled "five" is a .rtf file. "../" should call the hard disk right?
# 4  
Old 11-07-2010
The notation ../ indicates that the file is in the parent directory, rather than the current (or present) working directory.

Assuming you are running this from the command line (terminal), then you can test for the presence of the file with this command line command:

Code:
ls -al ../five

If that results in a file not found message, try removing the ../ to see if it is in the current directory. If it is, then remove those from your string in the C programme.
This User Gave Thanks to agama For This Post:
# 5  
Old 11-07-2010
You don't need xcode to compile these simple C programs. It's really overkill. gcc filename.c -o filename ; ./filename
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 11-07-2010
I'm on the right track now, I just need to study more.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. UNIX for Dummies Questions & Answers

Opening a file with vs. without "-e"

I tried opening .bash_profile using the following command: $ open .bash_profile Some people use the following form: $ open -e .bash_profile What is the difference between the two although I find the output is the same? Thanks. (2 Replies)
Discussion started by: Abder-Rahman
2 Replies

7. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question