LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-13-2008, 01:44 PM   #1
thefountainhead100
LQ Newbie
 
Registered: Mar 2008
Posts: 27

Rep: Reputation: 15
Unhappy Doubt in a simple basic C program....


Hi

I am new to C prog..

I just want to know what is the problem with the following code.

#define BUFSIZE 1024
int main(void)
{
char *s;
FILE *fp;
s = malloc (BUFSIZE);

fp = fopen("~/Documents/newtxt.txt", "rb");
fgets( s, BUFSIZE, fp);
printf("%s", s);
return 0;
}

I have a text file in ~/Documents names newtxt with a line of text in it. I just want to read it and print it on screen. It compiles OK but when i execute it gives "SEGEMENTATION FAULT: CORE DUMPED"

I also tried fgets(s,BUFSIZE-1,fp) above.. same result.

Your help is appreciated.
Thanks.
Ash
 
Old 03-13-2008, 02:00 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
You can’t pass the character ‘~’ as the home directory in your system calls. This only works if you have some sort of shell interpretation to handle your environment variables. You might have found this out if you bothered to check the return value of fopen().
 
Old 03-13-2008, 02:01 PM   #3
exscape
Member
 
Registered: Aug 2007
Location: Sweden
Distribution: OS X, Gentoo, FreeBSD
Posts: 82

Rep: Reputation: 15
Check if (!fp).
I'd bet that the ~ is the problem. Specify the full path and it should work.
 
Old 03-13-2008, 10:15 PM   #4
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by exscape View Post
Check if (!fp).
I'd bet that the ~ is the problem. Specify the full path and it should work.
Or use getenv("HOME").
 
Old 03-13-2008, 11:01 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Also, you should not assume char is one byte. Your malloc line should read:
Code:
s = malloc (BUFSIZE * sizeof(char));
And you should check that malloc worked too (check that s != NULL).
 
Old 03-13-2008, 11:21 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by matthewg42 View Post
Also, you should not assume char is one byte.
This is wrong. The C standard guarantees that
Code:
sizeof(char)
always evaluates to 1 (see §6.5.3.4.3). What one should not assume is that the number of bits in a char is 8.
 
Old 03-13-2008, 11:44 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Quote:
Originally Posted by osor View Post
This is wrong. The C standard guarantees that
Code:
sizeof(char)
always evaluates to 1 (see §6.5.3.4.3). What one should not assume is that the number of bits in a char is 8.
I didn't know that was in the standard. In that case you probably can assume it will be a byte.

If you're ultra-paranoid you might still want to check it since char could be set to something else using a macro, although I expect that would break lots of other stuff before you have to worry about it in your own code.
 
Old 03-14-2008, 10:02 AM   #8
simplicissimus
Registered User
 
Registered: Mar 2008
Posts: 104

Rep: Reputation: 15
source code

I would do it like this (formatting is last unfortunately after posting the message):

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUFSIZE 1024
int main(void)
{
char *s;
FILE *fp;
char filename[64];

s = malloc (BUFSIZE);
if (!s) {
printf ("malloc error\n");
return -1L;
}
sprintf (filename, "%s/Documents/newtxt.txt", getenv("HOME"));

fp = fopen (filename, "rb");
if (!fp) {
printf ("fopen error\n");
return -2L;
}
fgets (s, BUFSIZE, fp);
printf ("%s", s);
fclose (fp);
return 0L;
}


Hope this helps,
Regards,
SIMP

Linux Archive

Last edited by simplicissimus; 04-02-2008 at 05:28 AM.
 
Old 03-14-2008, 03:40 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by simplicissimus View Post
I would do it like this (formatting is last unfortunately after posting the message):

You could edit your post and put CODE tags around it?
 
Old 03-15-2008, 02:01 AM   #10
thefountainhead100
LQ Newbie
 
Registered: Mar 2008
Posts: 27

Original Poster
Rep: Reputation: 15
Thanks people. Problem solved. It was the '~'.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple C++ Program: Program Compiles But Won't Run (Segmentation Fault) violagirl23 Programming 3 01-09-2008 01:09 AM
ipv6 program doubt ayeshaseerin Linux - Networking 0 05-09-2006 08:09 AM
Linux Newbie: doubt regarding program files amit_chandak Linux - Newbie 5 03-04-2006 04:57 PM
Program to eject cdrom drive ... doubt tuxfood Programming 3 07-20-2005 04:33 AM
basic ncurses doubt .. related to raw() and cbreak() ... tuxfood Programming 2 08-28-2004 12:15 PM


All times are GMT -5. The time now is 09:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration