English 中文(简体)
采用(a)和(b)系统电话
原标题:Using read() and write() system calls
  • 时间:2012-05-13 03:17:40
  •  标签:
  • c
  • io

因此,我有一项任务,即Im应该用读写读写,从档案中读写,然后用文字写。 然而,我不能让它工作。

I have to call a sort function on buf first to sort the string (which I got using read) before I re-output it. Can I treat buf as an array? Or does it not work like that? The reason is because I have to sort the string first.

int record_compare(const void *a, const void *b)
{
  return (memcmp(a, b, num_bytes));
}
 qsort(buf, num_elements, num_bytes, record_compare);
 while (count < n - num_bytes)
 {
   i = memcmp(buf+count, buf+count + num_bytes, num_bytes);
   if (i == 0)
     count = count + num_bytes;
   else
   {
     for (k = 0; k < num_bytes; k++)
     {
   printf("%c", buf[count]);
   count++;
     }
   }     
 }

但是,由于我看上去,而不是像植被这样的东西,我仍然能够把香料当作一个阵列? 这就是这种类型如何在普通阵列上发挥作用(然后在不重复的情况下加以打印)。

 do {
     c = read(0, buf+n, 1);
     if (c != 0)
        n++;
   }
 while (c != 0);

这就是我如何做。

问题回答

<read( system calls will be used N bytes (1 byte at a time the way sent the Code). 你们在获得一条新路线之前,或者在你获得零的寄回(这标志着亲爱阵)之前,或者在你出现错误之前,都会读到。 假设你找到一条新路线,你将开始一个新的缓冲地带。

你也许有一系列的特性点,你也许会为每一行分配新的座标,并将点子存放在多个特性点。

enum { MAX_LINES = 1024 };
char *lines[MAX_LINES];
int n_lines = 0;

假定固定上限为科索沃;你也可以作出安排,积极分配点数。

因此,你需要一个功能来读一条线并加以储存。

然后,与你的职能相比,你有问题。 页: 1

qsort(lines, n_lines, sizeof(char *), record_compare);

But record_compare() will be given two char ** values, so you need to use:

int record_compare(const void *v1, const void *v2)
{
    const char *s1 = *(char **)v1;
    const char *s2 = *(char **)v2;
    return(strcmp(s1, s2));
}

编制分类的数据非常简单。 因此,你需要集中处理线读者的职能。





相关问题
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 ...

热门标签