How to open .ttcn file using C file open functions?

↘锁芯ラ 提交于 2019-12-11 15:12:47

问题


I am working on TTCN-3 (Testing and Test Control Notation) scripting language. I wanted to prepare on guideline checker for this code files.

For that I want to read lines of TTCN-3 script file( some thing like file.ttcn ) one by one into a buffer. But for me fopen / sopen / open / fgetc / fscanf are not able to work properly and are not reading the file correctly. It is giving NULL. Is there any way I can read characters of it into a buffer. I think C cannot read files with more than three extension characters (like .ttcn). Forgive me if my assumption is wrong.

My Environment is Turbo C on windows.

Edit:

Yes I checked those errors also but they are giving unknown error for read() and no such file or directory exists.

My code is as follows

#include <errno.h>
 #include <io.h>
 #include <fcntl.h>
 #include <sys\stat.h>
 #include <process.h>
 #include <share.h>
 #include <stdio.h>

 int main(void)
 {
    int handle;
    int status;
    int i=0;
    char ch;

    FILE *fp;
    char *buffer;
    char *buf;
    clrscr();
    handle = sopen("c:\\tc\\bin\\hi.ttcn", O_BINARY, SH_DENYNONE, S_IREAD); 

/here even I used O_TEXT and others/

    if (!handle)
    {
       printf("sopen failed\n");
   //    exit(1);
    }

   printf("\nObtained string %s @",buf);

    close(handle);

    fp=fopen("c:\\tc\\bin\\hi.ttcn","r");  \\sorry for the old version of one slash
   if(fp==NULL)                            \\I was doing it with argv[1] for opening 
   {                                       \\user given file name 
     printf("\nCannot open file");
   }
    ch=fgetc(fp);
   i=0;
   while(i<10)
   {
     printf("\ncharacter is %c  %d",ch,ch);
     i++;                                    //Here I wanted to take characters into 
     ch=fgetc(fp);                           //buffer   
   }
   getch();
   return 0;
}

回答1:


The most likely culprit is your Turbo C, an ancient compiler. It's techincally a DOS compiler, not Windows. That would limit it's RunTme Library to 8.3 filenames. Upgrade to something newer - Turbo C++ seems like a logical successor, but Microsoft's VC++ Express would work as well.




回答2:


Your assumption is wrong about extensions. If fopen is returning NULL, you should output the result of strerror(errno) or use the perror() function to see why it failed.

Edit: The problem is probably because you have "c:\tc\bin\hi.ttcn". in C, "\t" is interpreted as tab, for example.

You could do

"c:\\tc\\bin\\hi.ttcn"

But this is extremely ugly, and your system should accept:

"c:/tc/bin/hi.ttcn"



回答3:


MS-DOS does not know about long file names, thos including files with extensions longer than 3 characters. Therefore, the CRT provided by Turbo C most probably does not look for the name you are providing, but a truncated one - or something else.

Windows conveniently provides a short (i.e. matching the 8.3 format, most of the time ending in ~1 unless you play with files having the same 8-character prefix) file name for those; one way to discover it is to open a console window and to run "dir /x" in the folder your file is stored.

Find the short name associated to your file and patch it into your C source file.

Edit: Darn, I'll read the comments next time. All credits to j_random_hacker.




回答4:


Now that you've posted the code, another problem comes to light.

The following line:

fp=fopen("c:\tc\bin\hi.ttcn","r");

Should instead read:

fp=fopen("c:\\tc\\bin\\hi.ttcn","r");

In C strings, the backslash (\) is an escape character that is used to encode special characters (e.g. \n represents a newline character, \t a tab character). To actually use a literal backslash, you need to double it. As it stands, the compiler is actually trying to open a file named "C:<tab>c<backspace>in\hi.ttcn" -- needless to say, no such file exists!



来源:https://stackoverflow.com/questions/515171/how-to-open-ttcn-file-using-c-file-open-functions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!