Sponsored Content
Top Forums Programming read a file wich fscanf() in a function Post 302243329 by jim mcnamara on Saturday 4th of October 2008 08:20:54 PM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
VFSCANF(3P)						     POSIX Programmer's Manual						       VFSCANF(3P)

PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the correspond- ing Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. NAME
vfscanf, vscanf, vsscanf - format input of a stdarg argument list SYNOPSIS
#include <stdarg.h> #include <stdio.h> int vfscanf(FILE *restrict stream, const char *restrict format, va_list arg); int vscanf(const char *restrict format, va_list arg); int vsscanf(const char *restrict s, const char *restrict format, va_list arg); DESCRIPTION
The vscanf(), vfscanf(), and vsscanf() functions shall be equivalent to the scanf(), fscanf(), and sscanf() functions, respectively, except that instead of being called with a variable number of arguments, they are called with an argument list as defined in the <stdarg.h> header. These functions shall not invoke the va_end macro. As these functions invoke the va_arg macro, the value of ap after the return is unspecified. RETURN VALUE
Refer to fscanf(). ERRORS
Refer to fscanf(). The following sections are informative. EXAMPLES
None. APPLICATION USAGE
Applications using these functions should call va_end( ap) afterwards to clean up. RATIONALE
None. FUTURE DIRECTIONS
None. SEE ALSO
fscanf(), the Base Definitions volume of IEEE Std 1003.1-2001, <stdarg.h>, <stdio.h> COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2003 Edition, Standard for Information Technol- ogy -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html . IEEE
/The Open Group 2003 VFSCANF(3P)
All times are GMT -4. The time now is 05:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy