LinuxQuestions.org
Visit Jeremy's Blog.
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-23-2004, 01:05 PM   #16
BugZ[ru]
LQ Newbie
 
Registered: Mar 2004
Posts: 2

Rep: Reputation: 0
How to avoid?


Gentlemen, all what u say is somewhat clear.

But, can you explain:
1) (very interesting to me)
I've scanf(%s, somebuf); in my program, and how do I know, the size to allocate for incoming text?
In general, can I use scanf function and avoid SYGFAULT? If I can't - nor static, nor dynamic, what should I do to handle and allocate in memory incoming string with unknown size.
2) This code is sometime works:

char *string;
scanf("%s", string);

Smometimes works, sometimes SYGFAULTs, why?
I do not allocate any memory to it, how can it work?

Regards! =)
 
Old 03-23-2004, 02:37 PM   #17
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
As far as I know, you can't use scanf with the %s escape on untrusted input; there is no way to keep it from overflowing. You can use something like %50s, which says that you want to get at most 50 characters. I think that is 50+1 including the \0, but I didn't verify.

If that code snippet works, it is by chance. string had some uninitialized value in it, and it happened to be a memory address which was writable by you. This means you scribbled on a random part of your program's memory, and it might crash later on. Alternatively, the pointer could point to somewhere non-writable, in which case you will get a seg fault on the scanf(). This is one of the beautiful parts of debugging unititialized pointers: They may work fine until you change a completely unrelated bit of code, and then the program can start crashing.
 
Old 03-23-2004, 02:49 PM   #18
BugZ[ru]
LQ Newbie
 
Registered: Mar 2004
Posts: 2

Rep: Reputation: 0
Thanks for a clear explanation! =)
I successfully try to use read function instead of scanf like this:

chars = read(0, string, MAX_STRING_SIZE);

string[chars-1] = '\000';

and now it's clear with scanf. %)
 
Old 09-09-2005, 08:43 AM   #19
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
hey dudes, soz for opening a thread thats more than a year since it's last post.

I hope ppl are still active here.

i cant seem to be able to open a file.

the specific line that seems to enjoy trowing seg faults at me is bolded

FILE*myfile = (FILE*)malloc(sizeof(FILE)*3000);
if((myfile = fopen("getline.txt","tr"))==NULL)
{
printf("\nFile couldnt be opened\n");
}
else
{
printf("\nOpened file: Catalogue");
char temps[20];
fgets(temps,19,myfile);
printf(temps);
for(i=0;i<argv;i++)
{
printf("\nOpened file: Catalogue");
GetLineOI(&myfile);
fclose(myfile)
}
}

whats going on?

oh and incase your all wondering, i've been programming in C for about 2 hours a week for the last 6 weeks so im definantly a noob.

can anyone help or is my prgram doomed to failure?

me =
 
Old 09-09-2005, 08:48 AM   #20
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
oh dear! what's this!

Code:
 FILE*   myfile = (FILE*) malloc (sizeof(FILE)*3000);
you don't allocate space for the file pointer.
the C library worries about the file operations.
you just declare a FILE pointer,

FILE * myfile;
 
Old 09-09-2005, 08:49 AM   #21
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
oh and also i know the file getline.txt exsists cause i have checked.

it has one line of text in it and that is "This is a test"
 
Old 09-09-2005, 08:53 AM   #22
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
thanx dude

I changed it but myfile still == NULL when opened.

i know i worry ppl sometimes but that what noobs are for
 
Old 09-09-2005, 09:05 AM   #23
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
blimey

where do we start!
it's a bit of a mess, sorry!

Code:
fopen("getline.txt","tr"))
"tr" ? what mode is this? "r+" i take it?

Code:
 for(i=0;i<argv;i++)
argv?? don't you mean argc? argc is the count of args.

Code:
 char temps[20];
why so small? give yourself some room man!


Look at man perror
which will print out the error message like "file not found" etc.
 
Old 09-09-2005, 09:30 AM   #24
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
man thanx from the bottom of my heart, you really saved the day.

I changed the tr in fopen("getline.txt","tr") to t

and changed char temps[20]; to 100

your suggestion about the argv and c i dont get and when i changed it, the compiler spat the dummy.

I got tr from the lecture notes (opens a text file for reading).

argv i took straight from the note as well (im doing first year programing at UNI in Australia so dont complain )

for a code i wrote all by my self im pretty proud of it.

thanx again for the help.
 
Old 09-09-2005, 09:44 AM   #25
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
no no.

should be mode "+r"

look at man page for fopen.

do you know what you are doing with argv and argc?
 
Old 09-09-2005, 09:47 AM   #26
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
hehe nope wouldnt have a clue what argv and c are but i know that i just want to read from the file, not write to it too, thats a seperate part of the program
 
Old 09-09-2005, 06:42 PM   #27
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
AAAARRRRRGGG!

im now starting to get really anoyed with these lectures

i have:

char temp[100];
do
{
temp = fgetc(*myfile);
printf(temp);
}
while(temp)!=NULL);

yet it still segments, the warning i get while compiling is:

incompatible types in assignment

can anyone help, i have searched both Yahoo, AJ and google and not found any help.

the lecture notes say that this should be right but it doesnt seem to work.

Last edited by Goblin_C_Noob; 09-09-2005 at 06:53 PM.
 
Old 09-09-2005, 07:24 PM   #28
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
Quote:
printf(temp);
what exactly did you intend this to do?????

maybe this
Code:

printf("%c",temp);


that is a syntax error usage of the printf function.
 
Old 09-09-2005, 08:38 PM   #29
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
what im trying to do is read in a few lines of text from a file and print them to the screen
 
Old 09-09-2005, 09:41 PM   #30
Goblin_C_Noob
Member
 
Registered: Sep 2005
Posts: 48

Rep: Reputation: 15
99% fixed, now i just gotta stop the damned thing from printing out the last line, cant seem to get it to stop when it gets to EOF.

i might try some if statements in there

oh how i love java, much better than C
 
  


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
what does Segmentation Fault mean ? baronlynx Linux - Newbie 10 10-25-2009 04:32 PM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 09:34 AM
Help !!! Segmentation fault mola Linux - Software 3 06-23-2005 11:13 AM
Segmentation fault tejas15_10 Programming 9 06-20-2005 09:12 AM
Segmentation fault santhosh_o Programming 3 10-26-2004 05:45 AM

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

All times are GMT -5. The time now is 02:11 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