C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 2nd, 2012, 08:56 AM
rkrasowski rkrasowski is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 rkrasowski User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 30 sec
Reputation Power: 0
Readind from file that is updated

Hi
I am new to C and I find it fascinating. I do some programming in Perl that is much easier than C.
What I am trying to do in C is something very easy in Perl. I have a file that is constantly updated with data from GPS. I have script in C that read that file every second. The problem is that when I update that file while reading script is running I get "Segmentation fault (core dumped)" error. I did not have problem like this in Perl. How to solve it??

Here is my simple file reading script:

#include<stdio.h>
int main()
{
char message[1000];
FILE *file_in;
while(1)
{
file_in=fopen("test.txt", "r");
fscanf(file_in,"%s", message);
printf("%s\n", message);
fclose(file_in);
}
return 0;
}

Thanks in advance
Robert

Reply With Quote
  #2  
Old December 2nd, 2012, 12:01 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,831 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 13 h 49 m 45 sec
Reputation Power: 1774
Step 1 is make sure you post code within [code][/code] tags, so that people can stomach reading the code.

Step 2 is check for errors. Don't assume that the file was opened successfully.
Code:
file_in=fopen("test.txt", "r");
if ( file_in ) {
    // do stuff
    fclose( file_in );
} else {
    perror("Can't open file");
}


Step 3 - what is your input data format. If it's characters devoid of any white space at all, then %s will just overflow your buffer if you let it.
It's safer to use fgets, like
Code:
file_in=fopen("test.txt", "r");
if ( file_in ) {
    if ( fgets( message, sizeof(message), file_in ) != NULL ) {
        // do something with it
    }
    fclose( file_in );
}


Or more usually,
Code:
    while ( fgets( message, sizeof(message), file_in ) != NULL ) {
        // do something with each line
    }


Step 4, this will read from the beginning of the file each time. Do you perhaps want to read from where you got to last time?

Step 5, use sleep() to allow more time for data to accumulate in the file.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Readind from file that is updated

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This 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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap