读取要格式化和输出的文本文件(C编程)

时间:2014-06-07 05:35:04

标签: c arrays file-io formatting char

我正在为一个班级开设一个程序,它比我想象的要困难得多。 这是我第一次使用C语言,但我有一些Java经验,所以我理解了一般概念。

我的目标: 读取文件中包含一些格式要求的文本文件,将文件存储在数组中,应用格式并输出格式化文本到stdout。

问题: 在文件中阅读很简单,但我在格式化和输出方面遇到了问题。

挑战:

- 输入文件将以?width X,?mrgn Y或两者(其中X和Y为整数)开头。它们总是出现在文件的开头,并且每个都在一个单独的行上。

- 输出必须具有根据格式请求(宽度,边距)格式化的文本。

- 另外,还有一个第三格式命令,?fmt 打开/关闭,它可以在整个文本的任何位置出现多次,并打开/关闭格式。

- 只需几次捕获:如果未显示?width 命令,则会将格式设置视为关闭,并忽略任何?margin 命令。

- 如果出现?width 命令,则会考虑格式化。

- 文件可以包含必须删除的空格(作为制表符和空格),但仅限于格式化时。

- 动态内存分配不允许

第一个C程序容易吗?我的教授真是太可爱了。

我已经在这个代码上工作了好几个小时(是的,我知道它看起来不像)并且我没有取得什么进展所以如果你觉得这是一个挑战我会非常喜欢感谢任何形式的帮助。谢谢!

到目前为止,我的代码读入文本文件并正确格式化边距。似乎无法找到消除空白的好方法(我相信令牌化是要走的路)或者当行的长度超过?width命令时进行自动换行。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 #define MAX_LINE_LEN 133 /* max 132 char per line plus one extra for the new line char
 6 #define MAX_LINES 300 /* max 300 lines per input file  */
 7 #define MAX_CHARS 39900 /* max number of characters in the file */
 8 
 9 /* Initializes array used to store lines read in from text
10    file as well as output array */
11 char input[MAX_LINE_LEN];
12 char buffer[MAX_CHARS];
13 char word_wrap[MAX_LINE_LEN];
14 
15 /* Functions */
16 void parameters(char [], FILE *);
17 
18 /* Variables */
19 int width = 0;
20 int margin = 0;
21 
22 /*
23 argc is the count of input arguments
24 *argv is a pointer to the input arguments
25 */
26 int main (int argc, char *argv[])
27 {
28     /* Creates file pointer */
29     FILE *fp = fopen(argv[1], "r"); /* r for read */
30 
31     if (!fp) /* Error checking */
32     {
33         printf("Error: Could not open file");
34         return 0;
35     }
36         
37     /* Retrieves width and margin parameters from input file */
38     parameters(input, fp);
39 
40     fclose(fp);     /* Closes file stream */
41 
42     return 0;
43 }
44 
45 void parameters(char input[], FILE *fp)
46 {
47     /* Gets input file text line by line */
48     while (fgets (input, 133, fp) != NULL)
49     {
50         /* Creates a pointer to traverse array */
51         char *p = input;
52 
53         /* Checks for width parameter read in from text file */
54         if (input[0] == '?' && input [1] == 'w')
55         {
56                 strtok(input, " ");    /* Eliminates first token '?width'    */
57                 width = atoi(strtok(NULL, " "));    /* Stores int value of ASCII token
58                 p = NULL;
59         }
60 
61         /* Checks for margin parameter read in from text file */
62         if (input[0] == '?' && input[1] == 'm')
63         {
64             strtok(input, " ");                  /* Eliminates first token '?mrgn' */
65             margin = atoi(strtok(NULL, " "));    /* Stores int value of ASCII token
66             p = NULL;
67         }
68 
69         if (p != NULL)      /* skips printing format tokens at beginning of file */
70         {
71             if (width == 0)     /* no width command, formatting is off by default */
72             {
73                 printf("%s", p); /* Prints unformatted line of text */
74             }
75             else    /* formatting is on */
76             {
77                 printf("%*s" "%s", margin, " ",  p); /* Prints formatted line of text
78             }
79         }
80     }
81 }
82

这是一个示例输入文件,以及正确的输出:

?width 30
?mrgn 5
While there    are enough characters   here to
fill
    at least one line, there is
plenty
of
            white space which will cause
a bit of confusion to the reader, yet
the ?fmt off command means  that
    the original formatting of
the lines
    must be preserved. In essence, the
command ?pgwdth is ignored.

输出:

     While there are enough
     characters here to fill
     at least one line, there
     is plenty of white space
     which will cause a bit of
     confusion to the reader,
     yet the ?fmt off command means  that
         the original formatting of
the lines
    must be preserved. In essence, the
command ?pgwdth is ignored.

0 个答案:

没有答案