Моя программа ничего не экспортирует в файл .txt
; на самом деле, я даже не могу скомпилировать его в этой форме.Экспорт текста в TXT-файл в C
#include <stdio.h>
typedef struct /*We define structure type to save memory
(im not sure about this!!!) */
{
char name[20];
int num;
} cont;
void input(cont a[],int n) /*With this function i enter data in main program.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Insert name:");
scanf("%s",&a[i].name);
printf("Insert number:");
scanf("%d",&a[i].num);
printf("\n\n\n");
}
}
void export(cont a[],int n) /*Export data that is entered in main program to text file
output.txt that is in same folder as program*/
{
FILE *text;
int i;
text=fopen("output.txt","w");
fprintf(text,"Name:%s\nNumber:%d\n",a[i].name,a[i].num);
fclose(text);
}
void printinprog(cont a[],int n) /*This just prints data in program so we can check
that program works correctly.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Name:%s Number:%d",a[i].name,a[i].num);
printf("\n\n");
}
}
main()
{
cont per[20];
int c;
printf("Enter number of contacts:");
scanf("%d",&c);
input(per,c);
export(per,c);
system("pause");
}
Так что, когда я перехожу основную часть функции экспорта в printinprog функции выглядит следующим образом:
#include <stdio.h>
typedef struct /*We define structure type to save memory
(im not sure about this!!!) */
{
char name[20];
int num;
} cont;
void input(cont a[],int n) /*With this function i enter data in main program.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Insert name:");
scanf("%s",&a[i].name);
printf("Insert number:");
scanf("%d",&a[i].num);
printf("\n\n\n");
}
}
void printinprog(cont a[],int n) /*This just prints data in program so we can check
that program works correctly.*/
{
int i;
for(i=0;i<n;i++)
{
printf("Name:%s Number:%d",a[i].name,a[i].num);
printf("\n\n");
}
FILE *text;
text=fopen("output.txt","w");
fprintf(text,"Name:%s\nNumber:%d\n",a[i].name,a[i].num);
fclose(text);
}
main()
{
cont per[20];
int c;
printf("Enter number of contacts:");
scanf("%d",&c);
input(per,c);
printinprog(per,c);
system("pause");
}
Теперь программа работает, но я получаю неверные данные в файл .txt и выглядит следующим образом.
Так я уверен, что проблема заключается в ЬурейеЙ структуре на самом деле я думаю, что есть проблема с типами данных Потому что я определить его как продолжение и то не на самом деле существует так proggram оленьей кожи видеть, что в качестве текстовых данных и дает неверные данные в файл .txt.
Thx для помощи человеку просто быть понятным для других просто fprintf (текст, «Имя:% s \ nNumber:% d \ п», а [я] .name, а [я] .num); должен идти в петлю :) – Newbie
А, да. Извините, низко на кофеине в данный момент ... – palfrey
Хотя, если вы сделали fopen с режимом «a», это сработало бы (ужасно неэффективно, но это сработало бы ..) :) – palfrey