程序证明了一串文本。这里是我的代码(主要是justify.c):在GCC中编译C文件时出错(一个文件中有多个文件)
justify.c
#include <string.h>
#include "line.h"
#include "word.h"
#define MAX_WORD_LEN 20
int main(void)
{
char word[MAX_WORD_LEN+2];
int word_len;
clear_line();
for(;;)
{
read_word(word, MAX_WORD_LEN+1);
word_len = strlen(word);
if(word_len ==0)
{
flush_line();
return 0;
}
if (word_len >MAX_WORD_LEN)
word[MAX_WORD_LEN]='*';
if(word_len + 1 > space_remainding())
{
write_line();
clear_line();
}
add_word(word);
}
}
line.c
#include <stdio.h>
#include <string.h>
#include "line.h"
#define MAX_LINE_LEN 60
char line[MAX_LINE_LEN+1];
int line_len=0;
int num_words=0;
void clear_line(void)
{
line[0]='\0';
line_len=0;
num_words=0;
}
void add_word(const char *word)
{
if(num_words>0)
{
line[line_len]= ' ';
line[line_len+1]= '\0';
line_len++;
}
strcat(line,word);
line_len += strlen(word);
num_words++;
}
int space_remainding(void)
{
return MAX_LINE_LEN - line_len;
}
void write_line(void)
{
int extra_spaces, spaces_to_insert, i,j;
extra_spaces= MAX_LINE_LEN - line_len;
for(i=0; i< line_len; i++)
{
if(line[i] != ' ')
putchar(line[i]);
else
{
spaces_to_insert = extra_spaces/ (num_words - 1);
for(j=1; j<=spaces_to_insert +1; j++)
putchar(' ');
extra_spaces -= spaces_to_insert;
num_words--;
}
}
putchar('\n');
}
void flush_line(void)
{
if (line_len > 0)
puts(line);
}
word.c
#include <stdio.h>
#include "word.h"
int read_char(void)
{
int ch= getchar();
if (ch=='\n' || ch == '\t')
return ' ';
return ch;
}
void read_word(char *word, int len)
{
int ch, pos=0;
while((ch=read_char()) == ' ')
;
while(ch != ' ' && ch !=EOF)
{
if (pos<len)
word[pos++]=ch;
ch= read_char();
}
word[pos]= '\0';
}
line.h
#ifndef LINE_H
#define LINE_H
void clear_line(void);
void add_word(const char *word);
int space_remainding(void);
void write_line(void);
void flush_line(void);
#endif // LINE_H
word.h
#ifndef LINE_H
#define LINE_H
void clear_line(void);
void add_word(const char *word);
int space_remainding(void);
void write_line(void);
void flush_line(void);
#endif // LINE_H
当我编译所有这些代码块中它给了我没有错误。但是,当我做同样的GCC
的gcc -o证明justify.c line.c word.c
我得到这个:
justify.c:1:1: error: expected identifier or '(' before '<' token
<?xml version="1.0" encoding-"UTF-8" standalone="yes" ?>
我不能发现错误,我一直盯着这几个小时。请 我真的很感谢任何帮助,我可以得到。
看来你的编译器或'string.h'在你的操作系统中出了问题。尝试'g ++'而不是'gcc',如果这不起作用,我会尝试升级(重新安装)gcc – VolAnd