file-pointer

What is difference between file descriptor and file pointer? [duplicate]

风格不统一 提交于 2020-06-14 06:38:09
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What's the difference between a file descriptor and file pointer? If I open file like this: FILE *fp = fopen("mr32.txr","r"); then fp is file pointer or file descriptor? What is difference between them? 回答1: fp is a FILE pointer File pointer: It is high level interface Passed to fread() and fwrite() functions Includes buffering,error indication and EOF detection,etc. Provides higher portability and efficiency.

How do I get the file pointer that is being referenced by a file descriptor?

不羁岁月 提交于 2020-03-04 18:47:39
问题 I am trying to use dup2 to communicate between parent and child in the following code. #include <stdio.h> #include <unistd.h> void main(int argv, char *argc) { int testpipe[2]; pipe(testpipe); int PID = fork(); if (PID == 0) { dup2(testpipe[0], 0); close(testpipe[1]); execl("./multby", "multby", "3", NULL); close(testpipe[0]); close(0); } else { dup2(testpipe[1], 1); close(testpipe[0]); printf("5"); fclose(stdout); close(testpipe[1]); wait(NULL); } } I understand that fclose is needed to

Obtain filename from file pointer in C [duplicate]

删除回忆录丶 提交于 2019-12-18 08:33:59
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Getting Filename from file descriptor in C How get fileName having FILE*? Is there any way where I can find the file_name from a file-pointer in C? fp = fopen(file,"r"); From fp, is it possible to get the file name which I have opened? 回答1: I believe not, because a file* could be to something that doesn't even have a name. There might be a platform dependent way, depending I. Your system, so if you don't care

Using a C string like a FILE*

穿精又带淫゛_ 提交于 2019-12-08 14:44:22
问题 I have a C function that reads a stream of characters from a FILE* . How might I create a FILE* from a string in this situation? Edit: I think my original post may have been misleading. I want to create a FILE* from a literal string value, so that the resulting FILE* would behave as though there really was a file somewhere that contains the string without actually creating a file. The following is what I would like to do: void parse(FILE* f, Element* result); int main(int argc, char** argv){

Is there a file pointer (FILE*) that points to nothing?

≡放荡痞女 提交于 2019-12-02 02:49:42
问题 For some reasons I need a file pointer (FILE*) that points to nothing. It means I can pass it to fprintf function and fprintf ignore the file pointer. for example: void my_function() { FILE* nothing = NULL; // NULL is not correct. int n = fprintf(nothing, "PI = %f", 3.14); // Note: When nothing = NULL, error ocured. } Is there a file pointer (FILE*) that points to nothing? 回答1: No. Your code will cause a run-time exception. You can use /dev/null for example, if you're running on an OS that

C file size discrepency

独自空忆成欢 提交于 2019-12-02 02:37:23
问题 I am trying to learn C, and am currently working on a toy script. Right now, it simply opens a text file, reads it char by char, and spits it out onto the command line. I looked up how to see the size of a file (using fseek() and then ftell()), but the result it returns doesn't match up with the number I get from counting the characters in a while loop as I iterate through the file. I'm wondering if the discrepency is due to windows using \r\n and not just \n, since the discrepency seems to

Is there a file pointer (FILE*) that points to nothing?

梦想的初衷 提交于 2019-12-02 01:20:19
For some reasons I need a file pointer (FILE*) that points to nothing. It means I can pass it to fprintf function and fprintf ignore the file pointer. for example: void my_function() { FILE* nothing = NULL; // NULL is not correct. int n = fprintf(nothing, "PI = %f", 3.14); // Note: When nothing = NULL, error ocured. } Is there a file pointer (FILE*) that points to nothing? No. Your code will cause a run-time exception. You can use /dev/null for example, if you're running on an OS that supports it, but there's nothing like that built-in in C++. To solve your actual problem (stated in comments),

ftell on a file descriptor?

孤街浪徒 提交于 2019-11-29 16:39:56
问题 Is there a way to do what ftell() does (return the current position in the file) on a raw file descriptor instead of a FILE*? I think there ought to be, since you can seek on a raw file descriptor using lseek(). I know I could use fdopen() to create a FILE* corresponding to the file descriptor, but I'd rather not do that. 回答1: Just use: position = lseek(fd, 0, SEEK_CUR); 来源: https://stackoverflow.com/questions/3399236/ftell-on-a-file-descriptor

Obtain filename from file pointer in C [duplicate]

狂风中的少年 提交于 2019-11-29 14:21:58
Possible Duplicate: Getting Filename from file descriptor in C How get fileName having FILE*? Is there any way where I can find the file_name from a file-pointer in C? fp = fopen(file,"r"); From fp, is it possible to get the file name which I have opened? I believe not, because a file* could be to something that doesn't even have a name. There might be a platform dependent way, depending I. Your system, so if you don't care about portability, try looking at your compiler's definition of FILE if it has one. Odds are that your only way is to save the name when it's opened. There are some other

Working with file pointers on a csv

大兔子大兔子 提交于 2019-11-28 09:45:20
问题 I was wondering how to change the code below to read x number of lines only process the sql insert statement then continue to read the file by x number and process until end of file. I am new to the idea of file pointers but i understand it should be possible using fgets. I'm hoping to change the below code into a function where I can pass the filename and the number of lines I want read and processed. I currently have : (from here) $handle = fopen(dirname(__FILE__)."/files/workorderstest.csv