home hardware prices news articles forums photos user reviews
Go Back   Tech Support Forums - TechIMO.com > PC Hardware and Tech > Webmastering and Programming
Join TechIMO for Free!
Register Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read
Reply Get bargains at  »  Dealighted.com
 
Thread Tools
Currently Active Users: 1703
Discussions: 188,401, Posts: 2,243,608, Members: 232,631
Old April 15th, 2005, 01:45 AM   Digg it!   #1 (permalink)
Member
 
Indian's Avatar
 
Join Date: Aug 2004
Location: Hyderabad,India
Posts: 242
What is wrong with this C code ?

HI,
I am trying to read come numbers from a text file and than convert those numbers to floats.Here is my code,Everything looks fine but still I am not able to understand that why it is giving segmentation fault:-

Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

main()
{
    FILE *fs;
    char ch,d;
    char str[20];

    fs=fopen("text","r"); //I am reading a file :)
    int i;

    while((ch=fgetc(fs))!=EOF)
    {
        i=0; // assigning index to 0
        
        if(ch!='\t' ||ch!='\n')
      {
            str[i]=ch;
            i++;
            while((ch=fgetc(fs))!='\t')
            {

                str[i]=ch;
                i++;
            }

            str[i]='\0';
            
            func(str);
            
        }
        else
            continue;
    }
    fclose(fs);}

func(char *str)
{

        float a;

        a=atof(str);

        printf("%f",a);

}
I hope the code is clear to all

regards
__________________

Indian is offline   Reply With Quote
Old April 15th, 2005, 05:45 AM     #2 (permalink)
Member
 
Indian's Avatar
 
Join Date: Aug 2004
Location: Hyderabad,India
Posts: 242
well I have solved it ...there was some minor problems withthe code in while loops..

thanks for those 7 ppl(other than me, who atlest read this)

Indian is offline   Reply With Quote
Old April 15th, 2005, 07:15 AM     #3 (permalink)
Ultimate Member
 
elmers's Avatar
 
Join Date: Sep 2003
Location: Philadelphia
Posts: 1,462
Hey, I *just* woke up and actually compiled your code. Thats gotta be worth somehting.

In bloodshed two other errors were the 'late' definiton of func and that it doesnt return at least void.

elmers is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats Wrong With This Code Andybebad Webmastering and Programming 8 February 9th, 2005 04:39 AM
Help with a code UpYourz Webmastering and Programming 0 September 18th, 2003 10:53 AM
So what's wrong with this code? Emc2 Webmastering and Programming 15 January 22nd, 2003 01:07 AM
[code] tag displays wrong in Mozilla strangerstill Suggestion Box 2 June 24th, 2002 10:54 PM
What's wrong with this code? lost-and-found Webmastering and Programming 15 January 14th, 2002 12:06 PM

Most Active Discussions
Is It Just Me? (2906)
3-days in and no threads about Gaza (161)
Unarmed man on his stomach shot by .. (6)
New Build ( Finally ) (7)
CPU wont boot (7)
Building a gaming computer advice (5)
I think I just killed my computer w.. (24)
RCA 52Inch HDTV wont turn on (5)
Folderchat Weekday thread (444)
Recent Discussions
Laptop proccesor to desktop mob.. (2)
Please help! multiple problems! (4)
RCA 52Inch HDTV wont turn on (5)
New Build ( Finally ) (7)
Common Spyware Solutions (97)
How do you move a hard-drive to.. (4)
What is the best external enclo.. (0)
Partition Magic 7.0 (Unallocate.. (17)
For cheap price and good qualit.. (1)
Blackberry Storm, Gears of War .. (1)
Core 2 Quad Q9550 system (3)
COWBOOM Ripoff! Used Laptop w/$.. (4)


All times are GMT -4. The time now is 04:11 AM.
TechIMO Copyright 2008 All Enthusiast, Inc.



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28