LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Closed Thread
  Search this Thread
Old 09-04-2012, 03:09 AM   #1
stanleychao
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Rep: Reputation: Disabled
Question Can't open file with fopen. Help!


I am a starting learner. I enter the whole code from a book(headfirst c), as below, but I got "Segmentation fault (core dumped)".
Fedora 17 KDE,
gcc version 4.7.0 20120507 (Red Hat 4.7.0-5) (GCC)
I made those files, such as spooky.csv, with the same directory of compiled file. My gcc command is "gcc categorize.c -o categorize"
//categorize.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char line[80];
FILE *in = fopen("spooky.csv","r");
FILE *file1 = fopen("ufos.csv","w");
FILE *file2 = fopen("disappearances.csv","w");
FILE *file3 = fopen("others.csv","w");
while (fscanf(in,"%79[^\n]\n",line)==1)
{
if (strstr(line,"UFO"))
fprintf(file1,"%s\n",line);
else if (strstr(line,"Disappearance"))
fprintf(file2,"%s\n",line);
else
fprintf(file3,"%s\n",line);
}
fclose(in);
fclose(file1);
fclose(file2);
fclose(file3);
return 0;
}

after that, I thought must something wrong with 'fopen'. I make up another c file.
//test.c
#include<stdio.h>
#include<stdlib.h>

int main()
{
printf("Welcome enter this program1\n");
FILE *file1=fopen("/Home/Documents/cprogram/planA/others.csv","w");
printf("Welcome enter this program2\n");
if(file1!=NULL)
{
printf("3");
fprintf(file1,"%s","Here we are.");
printf("4");
}
else
{
printf("failed open");
return 1;
}
fclose(file1);
return 0;
}
And I got
"Welcome enter this program1
Welcome enter this program2
failed open"
So I thought must be fopen. Could anybody help me out.
 
Old 09-04-2012, 03:16 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
fopen sets the variable errno, and you can also use perror to print error message:
http://linux.die.net/man/3/perror
 
Old 09-04-2012, 03:21 AM   #3
stanleychao
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
I mean that I want the fopen works.
 
Old 09-04-2012, 03:42 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I mean you need to find the reason, I do not know what is the problem. perror may give some details and will be much easier to fix.
Probably you mistyped the filename, you have no permission to write that file or ???
 
Old 09-04-2012, 05:16 AM   #5
stanleychao
LQ Newbie
 
Registered: Aug 2012
Posts: 13

Original Poster
Rep: Reputation: Disabled
I got it. Thanks. Let me try first.
 
Old 09-04-2012, 05:48 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Duplicate of http://www.linuxquestions.org/questi...lp-4175425535/ in the programming section.
 
Old 09-04-2012, 06:09 AM   #7
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Moderator Response

Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate of http://www.linuxquestions.org/questi...lp-4175425535/
 
  


Closed Thread



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [clarification] File handling fopen, open skaushal_lk Programming 2 03-27-2012 11:17 PM
[SOLVED] PHP File open larger files or fopen files larger than 2gigs Nemus Linux - Server 6 02-18-2011 01:03 AM
fopen 2 gb file schneidz Programming 5 10-26-2007 10:24 AM
fopen: No such file or directory CatBreath Linux - Software 4 03-27-2005 06:33 AM
How to use fopen or something to open unicode filename Mathan Programming 10 09-23-2004 03:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:22 PM.

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