English 中文(简体)
用户文本文档中的Xcode C读物
原标题:Xcode C reading in user Text file
  • 时间:2012-04-10 16:28:43
  •  标签:
  • c
  • xcode

我正试图让用户输入文本档案的名称,然后打开该文本文档,并读到其中第一行,其中说明将读写多少条线并为其分配空间。 然后,我想在所分配的空间中储存其余线路。 我正在使用MacBook,并使用方案X代码。 我正在C实施该方案。

#include <stdio.h>      
#include <math.h>       
#include <stdlib.h> 


int main()
{
int MMSize, CacheSize, CBLineSize, SetAsso, NumOfLines, *Num;
char    RepPol, FileNam[75];
FILE    *ptr_file;

printf("Enter the name of the input file containing the list of memory references generated by the CPU: "); 

scanf( "%s", FileNam );
fpurge( stdin );

ptr_file = fopen(FileNam, "r");

 while (ptr_file==NULL){
    printf("failed to open file.
Please enter a valid file name: ");

    scanf( "%s", FileNam );
    fpurge( stdin );

    ptr_file = fopen( FileNam , "r" );
}   

fscanf(ptr_file, "%d", &NumOfLines);

Num = malloc(sizeof(int) * NumOfLines); 

for (int i=0; i<NumOfLines; i++) {
    fscanf(ptr_file, "%*[^0123456789]%d", &Num[i]);
            printf("%d
", Num[i]);
}

    free(Num);
fclose(ptr_file);

return 0;
}

我的《法典》是罚款的,但总是说投入文件是全国人民力量。 我已把投入文件作为我的项目源头上的1.txt。 我在名字档案中尝试打字。 档案和档案的整个存放地点都持续运作,因为我的文件是NUL。 然而,当我去找案人时,它有案文。 任何人都可以帮助我。

这部作品gu。 感谢大家的帮助。

最佳回答

这很可能更接近你们想要的东西。

#include <stdio.h>      
#include <math.h>       
#include <stdlib.h> 


int main(void)
{
  int  NumOfLines, *Num;
  char FileNam[75];
  FILE *ptr_file = NULL;

  printf("Enter the name of the input file containing the list of memory references generated by the CPU: "); 

  scanf( "%s", FileNam );
  fpurge( stdin );

  ptr_file = fopen(FileNam, "r");

  while(ptr_file == NULL) {
    printf("failed to open file.
Please enter a valid file name: ");

    scanf( "%s", FileNam );
    fpurge( stdin );

    ptr_file = fopen( FileNam, "r" );
  }   

  fscanf(ptr_file, "%d", &NumOfLines);

  Num = malloc(sizeof(int) * NumOfLines); 

  for (int i=0; i<NumOfLines; i++) {
    fscanf(ptr_file, "%d", &Num[i]);
    printf("%d
", Num[i]);
  }

  free(Num);
  fclose(ptr_file);
  return 0;
}

它将使用格式的投入文件:

2
123
456

我从你的法典中想到这种形式是什么。 现在发生的情况是,第一个数字代表的是更多的界限(不包括本身)。 之后,该方案试图读到许多字。

你犯的一些错误:

  • Calling fopen with a string literal instead of the variable with the same name
  • Using a do/while when you meant to use a while
  • Not dealing with pointers correctly when you allocated your Num array
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签