The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to read values that are passed to the shell function in ksh. prashant43 Shell Programming and Scripting 2 01-22-2008 11:21 AM
fscanf abey High Level Programming 4 02-14-2006 09:29 AM
read function rajashekaran UNIX for Dummies Questions & Answers 1 04-12-2002 05:55 PM
fscanf() j_t_kim High Level Programming 3 04-03-2002 08:59 AM
fscanf() j_t_kim High Level Programming 1 03-15-2002 11:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2008
cdbug cdbug is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 52
read a file wich fscanf() in a function

I use fopen, fscanf, fclose to read a file. It can work well. since many files should be read, a function is created with the same code. But in the function, fscanf can not work well.

for example, the first line of the the file is: > filename
but the fscanf will give: 207/23/eee/34
it appears to be a random address

Is it due to the function stack? Wish our guru to give some guidance

thanks
  #2 (permalink)  
Old 10-04-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,800
This will read a text file

Code:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
   if(argc > 1)
   { 
      char tmp[256]={0x0};
      FILE *in=fopen(argv[1], "r");
      if(in==NULL)
      {
           perror("Error opening input file")
           exit(1);
      }
      while (fgets(tmp, sizeof(tmp), in)!=NULL) printf("%s", tmp);
      if(feof(in) )
      {
               fclose(in);
               return 0;
       }    
       perror("File I/O error");
   }
   return 1;
}

regardless of format - fscanf is designed for reading in a file with a known format.
Without your code we cannot tell what is wrong.
  #3 (permalink)  
Old 10-06-2008
cdbug cdbug is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 52
I have solved the problem. Thanks
Closed Thread

Bookmarks

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

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




All times are GMT -4. The time now is 12:59 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0