LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 12: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, 01:00 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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, 01: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, 09: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, 10:01 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 10:21 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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, 10:44 PM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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, 09: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 04:28 AM.
 
Old 03-14-2008, 02:40 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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, 01:01 AM   #10
thefountainhead100
LQ Newbie
 
Registered: Mar 2008
Posts: 27

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


Reply



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 12:09 AM
ipv6 program doubt ayeshaseerin Linux - Networking 0 05-09-2006 07:09 AM
Linux Newbie: doubt regarding program files amit_chandak Linux - Newbie 5 03-04-2006 03:57 PM
Program to eject cdrom drive ... doubt tuxfood Programming 3 07-20-2005 03:33 AM
basic ncurses doubt .. related to raw() and cbreak() ... tuxfood Programming 2 08-28-2004 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 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
Open Source Consulting | Domain Registration