c++ - 在我的 C 代码中使用 scanf 进行命令行重定向
问题描述
我的代码没有读取我想要的文件。是的,我正在使用./a.out < filename
它来运行它,但它仍然不读取文件。循环正在工作,但它没有读取文件并执行我告诉它的内容。我scanf
错了吗?
我只包含了一部分代码。循环中还有更多内容,这些是来自结构的数组。
int main()
{
char ch;
int count_e = 0;
int count_c = 0 ;
struct customer arr_cust[50];
struct employee arr_emp[50];
//char
//cout << "hi";
//scanf("%c", &ch);
while(scanf("%c", &ch))
{
//scanf(" %c", &ch);
//cout << "yp"; (This is a test. it prints out and the loop is continuous)
//scanf("%c", &ch);
cin >> ch;
cout << ch;
if (ch == 'e')
{
cin >> ch;
cout << "test";
++count_e;
cin >> arr_emp[count_e].emp_id;
scanf("%s ", arr_emp[count_e].emp_name); //cin >> arr_emp[count_e].emp_name;
cout << arr_emp[count_e].emp_id << " ";
printf("%s ", arr_emp[count_e].emp_name);
}
if (ch == 'c')
{
++count_c;
cin >> arr_cust[count_c].cust_id;
scanf("%s ", arr_cust[count_c].cust_name); //cin >> arr_cust[count_c].cust_name;
cin >> arr_cust[count_c].cust_bal;
printf("%s ", arr_cust[count_c].cust_name);
cout << arr_cust[count_c].cust_name << " ";
cout << arr_cust[count_c].cust_id << " ";
//++count_c;
}
if (ch == 't')
{
int e;
int c;
char type;
float amount;
cin >> c;
cin >> e;
cin >> type;
cin >> amount;
int index_c;
int index_e;
for(int k =0; k < count_c; ++k)
{
if(c == arr_cust[k].cust_id)
{
index_c = k;
}
}
for(int j =0; j < count_e; ++j)
{
if(e == arr_emp[j].emp_id)
{
index_e = j;
}
}
printf("%s ", arr_cust[index_c].cust_name);
//cout << arr_emp[index_e].emp_name << " ";
printf("%s ", arr_emp[index_e].emp_name);
//cout << arr_cust[index_c].cust_name << " ";
if(type == 'w')
{
cout << "-$";
cout << amount << " ";
arr_cust[index_c].cust_bal -= amount;
cout << " $ " << arr_cust[index_c].cust_bal << endl;
// printf(" $ %s ", arr_cust[index_c].cust_bal);
}
else
{
cout << "+$";
cout << amount << " ";
arr_cust[index_c].cust_bal += amount;
cout << " $ " << arr_cust[index_c].cust_bal << endl;
// printf(" $ %s ", arr_cust[index_c].cust_bal);
}
}
return 0;
}
我试图读取的文件是
e 5 Elden
c 3 Felipe 55342.51415
e 3 Leonardo
e 1 Yong
c 9 Alessandra 8114.541862
c 6 Marnie 15287.78233
e 8 Kourtney
c 2 Lou 95053.44742
c 5 Numbers 51245.66138
e 4 Jarvis
e 9 Marlen
e 10 Florance
c 1 Devon 56442.27875
e 2 Elliott
c 8 Justina 73723.84849
c 10 Reyna 82946.53205
e 6 Antonetta
e 7 Florene
c 4 Merrill 98281.82784
c 7 Marlana 33252.21805
t 1 8 w 4924.86
t 9 6 d 3220.42
t 6 1 w 127.62
t 9 8 w 5566.7
t 9 8 d 5414.55
t 5 7 w 9422.35
t 9 10 d 1382.07
t 4 7 d 6131.07
t 8 2 w 2362.22
t 8 10 d 5834.48
t 5 4 w 5150.73
t 6 2 d 3795.96
t 1 9 w 3919.45
t 5 2 w 5037.31
t 3 9 w 8129.21
t 8 6 d 1235.67
t 2 4 d 6901.28
t 8 10 d 5599.44
t 6 9 d 1936.16
t 7 9 d 7363.98
解决方案
推荐阅读
- excel - 相同的电子表格结构和公式,但数据不同
- php - 使用 Google Translation API 的批处理在文件结束前停止
- xml - 十六进制值不能包含在名称 vb.net 中
- python - Django - UpdateView - 空选择字段?
- oracle - XML 解析 PL/SQL
- sql - 根据日期计算某行与其他行的最大值之差
- php - Laravel 将路由转发到另一个路由文件
- python - 使用python删除字符串中指定索引位置的字符
- objective-c - 如何在 Objective-C 中检查一个字符串是否包含另一个字符串?字符串包含 URL
- c# - 在电子邮件中嵌入 HTML img 的附件