C 在fopen()上没有这样的文件或目录
我有一个程序,我正在编写一个全局定义的文件变量,在它派生出一个子变量后,我试图让父变量访问它。但是,子对象是写入文件的对象,因此当我尝试将其作为父对象读取时,会出现C 在fopen()上没有这样的文件或目录,c,file,fork,C,File,Fork,我有一个程序,我正在编写一个全局定义的文件变量,在它派生出一个子变量后,我试图让父变量访问它。但是,子对象是写入文件的对象,因此当我尝试将其作为父对象读取时,会出现错误:没有这样的文件或目录。只是它没有作为错误抛出,而是存储在tmpFP文件中。我不知道该怎么解决这个问题 为了便于阅读,我省略了一些代码,对套接字的引用来自一个自定义库,假设可以。相关评论应该在所有CAP中,它们指出了我认为问题出现的地方 #include <stdlib.h> #include <stdio.h&
错误:没有这样的文件或目录
。只是它没有作为错误抛出,而是存储在tmpFP文件中。我不知道该怎么解决这个问题
为了便于阅读,我省略了一些代码,对套接字的引用来自一个自定义库,假设可以。相关评论应该在所有CAP中,它们指出了我认为问题出现的地方
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include "Socket.h"
#include "ToUpper.h" /* definitions shared by client and server */
#define LINE_SIZE 50
#define MAX_WORDS 10
#define MAX_LINE 1024
#define MAX_TMP 100
#define MAX_ARGS 4 /* allows program name + 3 positional parameters */
#define MIN_ARGS 2 /* must have at least 1 positional parameter */
#define NUM_PARMS 4 /* allows program name + 2 parameters + NULL */
#define ERR_RETURN -1
/* variables to hold socket descriptors */
ServerSocket welcome_socket;
Socket connect_socket;
char new_line[MAX_LINE];
char tmp_name[MAX_TMP]; //DECLARING FILENAME
char id_str[MAX_TMP];
char arr[LINE_SIZE]={0};
int id;
FILE *fp; //FILE USED IN CHILD PROCESS
void toupper_service(void);
int main(int argc, char* argv[])
{
FILE *tmpFP; //FILE USED IN PARENT PROCESS
pid_t spid; /* pid_t is typedef for Linux process ID */
int c=0,index=0;
id = (int) getpid();
sprintf(id_str, "%d", id);
strcpy(tmp_name,"tmp");
strcat(tmp_name, id_str);
if (argc < 2)
{
printf("No port specified\n");
return (-1);
}
welcome_socket = ServerSocket_new(atoi(argv[1]));
connect_socket = ServerSocket_accept(welcome_socket);
Socket_close(welcome_socket);
while (c!=EOF)
{
while((c=Socket_getc(connect_socket))!='\n')
{
arr[index]=c;
index++;
}
arr[index]='\0';
/* accept an incoming client connection; blocks the
* process until a connection attempt by a client.
* creates a new data transfer socket.
*/
spid = fork(); /* create child == service process */
if (spid == -1)
{
perror("fork");
exit (-1);
}
if (spid == 0)
{/* code for the service process */
toupper_service();
Socket_close(connect_socket);
exit (0);
} /* end service process */
else /* daemon process closes its connect socket */
{
waitpid(spid,NULL,0);
//PASSES THIS TEST SOMEHOW
if((tmpFP = fopen (tmp_name, "r")) == NULL)
{
fprintf(stderr, "%s\n",tmp_name);
fprintf (stderr, "error opening tmp file\n");
exit (-1);
}
while((c=fgetc(tmpFP))!=EOF)
{
//PRINTS OUT ERROR HERE A LETTER AT A TIME
fprintf(stderr, "c: %d %c\n", c, (char)c);
Socket_putc(c, connect_socket);
}
remove(tmp_name);
Socket_putc('\0', connect_socket);
Socket_close(connect_socket);
/* reap a zombie every time through the loop, avoid blocking*/
}
}/* end of infinite loop for daemon process */
fprintf(stderr, "C: %d\n",c);
}
void toupper_service(void)
{
int i=0, c, pointer,num_words=0,index=0;
int too_many_words=0;
char *word[MAX_WORDS]={NULL};
//THIS IS THE OTHER PLACE TMP_NAME IS USED
fp = freopen(tmp_name, "w", stdout);
while ((c=arr[index])!='\0')
{
if(c==' '||c=='\t'||c=='\n') //word encountered
{
if(num_words>=MAX_WORDS-1)
{
printf("Too many commands passed\n");
too_many_words=1;
exit(0);
break;
}
arr[index]='\0';
word[num_words]=&arr[pointer];
pointer=i+1;
num_words++;
}
index++;
}
word[num_words]=NULL;
if(too_many_words==0)
{
c=0;
int error=execvp(word[0],word);
}
return;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“Socket.h”
#包括客户端和服务器共享的“ToUpper.h”/*定义*/
#定义行大小为50
#定义最多10个单词
#定义最大行1024
#定义最大TMP 100
#定义最大参数4/*允许程序名+3个位置参数*/
#define MIN_ARGS 2/*必须至少有一个位置参数*/
#define NUM_PARMS 4/*允许程序名+2个参数+NULL*/
#定义ERR_RETURN-1
/*用于保存套接字描述符的变量*/
ServerSocket欢迎\u套接字;
插座连接插座;
新字符行[最大字符行];
字符tmp_名称[最大tmp]//声明文件名
字符id_str[MAX_TMP];
char arr[LINE_SIZE]={0};
int-id;
文件*fp//子进程中使用的文件
无效服务(无效);
int main(int argc,char*argv[])
{
FILE*tmpFP;//父进程中使用的文件
pid_t spid;/*pid_t是Linux进程ID的类型定义*/
int c=0,index=0;
id=(int)getpid();
sprintf(id_str,“%d”,id);
strcpy(tmp_名称,“tmp”);
strcat(tmp_名称、id_str);
如果(argc<2)
{
printf(“未指定端口”);
返回(-1);
}
welcome_socket=ServerSocket_new(atoi(argv[1]));
connect\u socket=ServerSocket\u accept(欢迎\u socket);
插座关闭(欢迎插座);
而(c!=EOF)
{
而((c=Socket\u getc(connect\u Socket))!='\n')
{
arr[指数]=c;
索引++;
}
arr[index]='\0';
/*接受传入的客户端连接;阻止
*处理,直到客户端尝试连接为止。
*创建新的数据传输套接字。
*/
spid=fork();/*创建子进程==服务进程*/
如果(spid==-1)
{
佩罗尔(“福克”);
出口(-1);
}
如果(spid==0)
{/*服务进程的代码*/
toupper_服务();
插座关闭(连接插座);
出口(0);
}/*最终服务流程*/
else/*守护进程关闭其连接套接字*/
{
waitpid(spid,NULL,0);
//不知怎么通过了这个测试
if((tmpFP=fopen(tmp_名称,“r”))==NULL)
{
fprintf(标准,“%s\n”,tmp_名称);
fprintf(stderr,“打开tmp文件时出错”);
出口(-1);
}
而((c=fgetc(tmpFP))!=EOF)
{
//在这里一次打印一封信
fprintf(标准,“c:%d%c\n”,c,(char)c);
插座(c,连接插座);
}
删除(tmp_名称);
插座插座('\0',连接插座);
插座关闭(连接插座);
/*每次通过循环都会收获一个僵尸,避免阻塞*/
}
}/*守护进程无限循环的结束*/
fprintf(标准,“C:%d\n”,C);
}
作废托珀_服务(作废)
{
int i=0,c,指针,num_words=0,index=0;
int太多单词=0;
字符*字[MAX_字]={NULL};
//这是使用TMP_名称的另一个地方
fp=FROPEN(tmp_名称,“w”,标准输出);
而((c=arr[index])!='\0')
{
if(c=''| | c='\t'| | c='\n')//遇到单词
{
如果(num_words>=MAX_words-1)
{
printf(“传递的命令太多\n”);
单词太多=1;
出口(0);
打破
}
arr[index]='\0';
单词[num_words]=&arr[指针];
指针=i+1;
num_words++;
}
索引++;
}
单词[num_words]=NULL;
if(太多的单词==0)
{
c=0;
int error=execvp(字[0],字);
}
返回;
}
看起来像是错误:子进程或其子进程之一未将此类文件或目录写入文件。您的代码指示子级将其用作标准输出。手动运行子执行vp
的程序并查看它写入stdout的内容会很有用。捕捉得好,指针变量没有初始化