read a file wich fscanf() in a function


 
Thread Tools Search this Thread
Top Forums Programming read a file wich fscanf() in a function
# 1  
Old 10-04-2008
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  
Old 10-04-2008
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  
Old 10-06-2008
I have solved the problem. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Bash] Read History function & Read Arrowkeys

Hi. How can I create a history function? (By "read" command or so) & How can I configure a read command so that the arrow keys are not displayed so funny? (^[[A) Thanks in advance. (4 Replies)
Discussion started by: sinnlosername
4 Replies

2. UNIX Desktop Questions & Answers

AIX how to read the file in function again and again

dear friends I have a wrote a shell script which works like this. 1.) a command is executed and the log is moved in the file. 2.) this file is copied in to the other file. 3.) used a grep command to find a particular word. 4.) if a particular word is there then the script will go to next... (4 Replies)
Discussion started by: aboy212u
4 Replies

3. Homework & Coursework Questions

C++ Attempting to modify this function to read from a (;) semi-colon-separated file

After some thought. I am uncomfortable issuing my professors name where, there may be unintended side effects from any negative responses/feedback. Willing to re post if I can omit school / professor publicly, but can message moderator for validation? I am here for knowledge and understanding,... (1 Reply)
Discussion started by: briandanielz
1 Replies

4. Shell Programming and Scripting

Read Table,View,Package,Function and Procedure Name in a File

Hi I am new to Unix shell scripting. But i need help to slove the below issue. Issue description: I want to read table, view names and package names in a file my plan to find the table name is : search "From" key word find the table or view To find the packge name : Search "Package... (5 Replies)
Discussion started by: sboss
5 Replies

5. Programming

fscanf: read words from file

Hi I have a file like that: 1 2 3 4 5 6 7 8 and I want print on stdout: 1 3 8 in other words i want choose what print out. I was thinking to use fscanf as: fscanf(file_in,"%d %d %d",&a, &b,&c); but in this way i get: 1 2 3 Is there a solution using fscanf to obtain my... (2 Replies)
Discussion started by: Dedalus
2 Replies

6. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

7. Programming

fscanf

Hi, Can any one tell me what "%hx" as control parameter mean in fscanf. Thanks, abey (4 Replies)
Discussion started by: abey
4 Replies

8. Programming

fscanf()

I keep trying to use fscanf() and for some reason I can't get the syntax down and always get seg faults. I'm on a SunOS 5.5.1, and my current code looks like this: int n1, n2, n3, n4, n5, n6; /* open config file */ if (fileptr = fopen(filename,"r") == NULL) { printf("couldn't open... (3 Replies)
Discussion started by: j_t_kim
3 Replies

9. Programming

fscanf()

thanks to everyone for your earlier replies, but i have yet another problem with file i/o. i'm trying to read multiple lines with the same file, and have been using the following code to take in the first two lines from a file... fscanf(fileptr, "%d %d %d %d %d %d\n", &n1, &n2, &n3, &n4, &n5,... (1 Reply)
Discussion started by: j_t_kim
1 Replies
Login or Register to Ask a Question