C-文本文件写入详细信息

时间:2017-05-30 14:42:28

标签: c file

我尝试做一些简单的事情来写一些我从用户那里得到的细节到一个txt文件然后它可以工作但是我希望它们在同一行,并且由于某种原因,我写的最后一个是在下一行

printf("\nPassengers Details:\n===================\n");

printf("Please Enter Passenger`s Passport-->:");
scanf("%d", &p1.passportnum);
getchar();
printf("\nPlease Enter Passenger`s First NAME-->:");
fgets(p1.firstname, SIZE, stdin);
printf("\nPlease Enter Passenger`s Last NAME-->:");
fgets(p1.lastname, SIZE, stdin);

fpassengers = fopen("passengers.txt", "w");
fprintf(fpassengers,"%d %s %s", p1.passportnum, p1.firstname, p1.lastname);

fclose(fpassengers);

在文件中,它将前2个写入同一行,last name写入下一行。

如何让它们在同一条线上?

1 个答案:

答案 0 :(得分:1)

您只需要在import { Injectable } from '@angular/core'; import { AngularFireDatabase, FirebaseObjectObservable } from 'angularfire2/database'; export class Lesson { constructor(public id: number, public pages: Object[]) { } } @Injectable() export class LessonService { private lessons: Lesson[] = []; public buttons: object[]; constructor(public db: AngularFireDatabase) { this.db.object('/lessons', { preserveSnapshot: true }).subscribe(lessons => { this.lessons = []; lessons.forEach(lesson => { let pages = []; if(lesson.val()['pages']) { Object.keys(lesson.val()['pages']).forEach(key => { let page = { id: key, title: lesson.val()['pages'][key]['title'], content: lesson.val()['pages'][key]['content'] }; let buttons = lesson.val()['pages'][key]['buttons']; if(buttons) { page['buttons'] = []; Object.keys(buttons).forEach(key => { page['buttons'].push({ content: buttons[key]['content'], icon: buttons[key]['icon'], id: key }); }); } pages.push(page); }); } this.lessons.push(new Lesson( lesson.key, pages )); }); }); } getLessons() { return this.lessons; } getLesson(id: number): Lesson { return this.lessons.filter(lesson => { return lesson['id'] == id; })[0]; } } \n的最后位置将\0替换为p1.firstname。这是一个通用的例子:

p1.lastname

您还必须检查fopen的返回值。