Exporting text to .txt file in C - c

My program doesn't export anything to the .txt file; actually, I can't even compile it in this form.
#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");
}
So when i move main part of export function to printinprog function it looks like this:
#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");
}
Now program works but i get wrong data in .txt file and that looks like this.
So i am pretty sure that problem is in typedef struct actually i think there is a problem with data types cuz i define it like cont and that doesnt actually exist so proggram doesnt see that as a text data and the gives wrong data to .txt file.

Problem is that
FILE *text;
text=fopen("output.txt","w");
fprintf(text,"Name:%s\nNumber:%d\n",a[i].name,a[i].num);
fclose(text);
should be inside the loop, and it's outside it. What's happening is that it uses the last value for i (n) and is essentially printing from random memory locations....

Related

how to do a delete and modify function correctly

This code has several issues that are beyond my understanding.
First, no matter how hard I try, the variable ok doesn't get saved in the file, thus ruining the display. It is strange because other variables work, it is just that one that doesn't seem to be working, even if I change the order.
Also, the delete and modify functions aren't working, as well.
I tried everything, but nothing seems to be working.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
// Structure of the employee
struct emp {
char name[50];
char ok[50];
int hr;
int age;
int id;
};
FILE *f;
emp* add(emp *e,int n){
int i;
char p;
emp *t;
int k;
t=(emp*)malloc(n*sizeof(int));
f=fopen("Data.txt","w");
for(i=0;i<n;i++){
printf("\nEnter Name : ");
scanf("%s",(t+i)->name);
printf("\nEnter Departement: ");
scanf("%s",&(t+i)->ok);
printf("\nEnter Age : ");
scanf("%d",&(t+i)->age);
printf("\nEnter avearge hour of works(per week) : ");
scanf("%d",&(t+i)->hr);
// k = ( ( (t+i)->hr ) *40 );//40 is minimum wage for each hour of work
// printf("Salary of The employe: %d\n",k);
printf("\nEnter EMP-ID : ");
scanf("%d",&(t+i)->id);
fprintf(f,"%s\n%s\n%d\n%d\n%d\n",(t+i)->name,(t+i)->ok,(t+i)->age,(t+i)->hr,(t+i)->id);
}
fclose(f);
return t;
}
void aff(emp *t,int n){
int I;
f=fopen("Data.txt","r");
for(i=0;i<n;i++){
if (f != NULL){
fscanf(f,"%s",&(t+i)->name);
fscanf(f,"%s",&(t+i)->ok);
fscanf(f,"%d",&(t+i)->age);
fscanf(f,"%d",&(t+i)->hr);
fscanf(f,"%d",&(t+i)->id);
printf("Name : %s\n",(t+i)->name);
printf("departement : %s\n",(t+i)->ok);
printf("Age : %d\n",(t+i)->age);
printf("Hours : %d\n",(t+i)->hr);
printf("ID : %d\n",(t+i)->id);
}
}
fclose(f);
}
//emp* modf(emp *t,int n){
// int i;
// int k;
// char nv[50];
// printf("id of the entry you want to be modified" );
// scanf("%d\n",&k);
// for(i=0;i<n;i++){
// if(k==(t+i)->id){
// scanf("%s",&nv);
// (t+i)->name=nv;
// }
// }
// return t;
//}
//void del(emp *t,int n){
// int i;
// emp a;
// int k;
// printf("position of the entry you want to delete?");
// scanf("%d",&a);
// for(i=0;i<n;i++){
// if (a == *(t+i)) {
// for(i=k;i<n-1;i++){
// *(t+i)=*(t+i+1);
// }
// }
// }
//}
int main(int argc, char *argv[]){
int c;
emp e;
emp *k;
k=&e;
int n;
emp *t;
t=&e;
char p;
ka:
printf("Welcome To The employe management Menu\n");
printf("1.Add employes?\n");
printf("2.show all employes?\n");
printf("3.delete an entry?\n");
scanf("%d",&c);
switch (c){
case 1:
ed:
printf("How many employes you want to add?\n");
scanf("%d",&n);
add(k,n);
if(p=='y'){
goto ed;
}
else goto ka;
break;
case 2:
aff(t,n);
break;
case 3:
del(t,n);
break;
}
return 0;
}
At least these issues:
Wrong allocation size #Weather Vane
Avoid allocation errors. Allocate to the size of the refenced object, not the type.
// t=(emp*)malloc(n*sizeof(int));
t = malloc(sizeof t[0] * n);
Easier to code right, review and maintain.
Uninitialized p #Craig Estey
f(p=='y') is undefined behavior as p is uninitialized.
This hints that OP is not compiling with all warnings enabled. Save time - enable all warnings.
emp not defined
Perhaps OP is not using a C compiler, but a C++ one?
i not defined in for (i = 0; i < n; i++) {
Is int I; the true code?
Bad "%s"
Without a width, "%s" is worse than gets(). Use a width like "%49s".

My C program is recursing infinitely and I cannot figure out why

This program is supposed to take a name from a user and output it backwards. Our professor said we had to use recursion and I am struggling with getting to not recurse infinitely. Please help.
#include <stdio.h>
#include <string.h>
void reversedWord(char input[],int size)
{ int count;
int count2;
if(count==0) {
count2=size;
}
count2=count2-1;
char store;
store=input[count2];
input[count2]=input[count];
input[count]=store;
count=count+1;
if(count>size)
{
return;
}
return reversedWord(input,size);
}
int main()
{
char input[100];
printf("Please enter your name: ");
scanf("%s",input);
int size=strlen(input);
reversedWord(input,size);
printf("\n Your name backwards: %s \n",input);
}
First in the above code, you need to initialize your variables count and count2.
The statements int count; means that you are declaring the variable count of integer type. But it doesn't hold any value.
for this example to have the recursion working, you need to pass 3 parameters(input string, start_index,end_index) at each call to the function.
Here's the modified code to have this working
void reversedWord(char input[],int start,int end)
{
end=end-1;
char store;
store=input[end];
input[end]=input[start];
input[start]=store;
start=start+1;
if(start>=end)
{
return;
}
return reversedWord(input,start,end);
}
int main()
{
char input[100];
printf("Please enter your name: ");
scanf("%s",input);
int size=strlen(input);
reversedWord(input,0,size);
printf("\n Your name backwards: %s \n",input);
}
Since you use counters inside your recursive function they should be static, I suggest reading documentation about what static does in the C language if it is new to you.
Below is your code with working recursion, but your algorithm for word reversion contains an error. I believe you can solve it yourself with a bit of thought, so I will just give a hint - it reverses the word, but then reverses it back right again.
#include<stdio.h>
#include<string.h>
void reversedWord(char input[],int size)
{
static int count = 0;
static int count2;
if(count==0) {
count2=size;
}
count2=count2-1;
char store;
store=input[count2];
input[count2]=input[count];
input[count]=store;
count=count+1;
if(count>size)
{
return;
}
reversedWord(input,size);
return;
}
int main()
{
char input[100];
printf("Please enter your name: ");
scanf("%s",input);
int size=strlen(input);
reversedWord(input,size);
printf("\n Your name backwards: %s \n",input);
}
I think even if you assign zero to count and count2 ,this will not work because you are calling the function recursively.
So every time you call reversedWord(input,size) the values are again initialized;
> void reversedWord(char input[],int size) { int count; //initialized
> for every recursive call
> int count2; //initialized for every recursive call
> if(count==0) {
> count2=size;
> }
> count2=count2-1;
> char store;
> store=input[count2];
> input[count2]=input[count];
> input[count]=store;
> count=count+1;
> if(count>size)
> {
> return;
> }
> return reversedWord(input,size); }
A better solution would be to declare these two variables count and count2 as static.
static int count,count2 ;
by default the static variable are assigned as 0 and they share a common memory,so no new space is assigned to them and the new values get reflected on the same variable(initialized just once)

Structure printf stops the program from working

I created the following code, it compiles but it doesen't run properly. It shows some imput witch is correct, but after that, instead of printing according to the line printf(prod[0].dr[0]); it enters an "not responding" state. If you instead write printf(prod[0].dr); it works perfectly. I need to be able to print each character individualy (so I need something like printf(prod[0].dr[0]); that works)
If the question is to broad, please comment and I will try to specify every detail.
Thanks!
#include <stdio.h>
FILE *f, *g;
struct productie
{
char st, dr[20];
int realizabil;
}prod[30];
int citire(FILE *f){
char sir[100];
int i=0,j;
while(!feof(f))
{
fgets(sir,100,f);
prod[i].st=sir[0];
for(j=3;j<strlen(sir);j++)
prod[i].dr[j-3]=sir[j];
i++;
}
return i;
}
int exista(char sir[],char c)
{
int i;
for(i=0;i<strlen(sir);i++)
{
if(c==sir[i])
return 1;
}
return 0;
}
void neterminale(struct productie p[],int n, char N[])
{
int k=0;
int i,j;
for(i=0;i<n;i++)
{
if(isupper(p[i].st) && !exista(N,p[i].st))
N[k++]=p[i].st;
for(j=0;j<strlen(p[i].dr);j++)
if(isupper(p[i].dr[j]) && !exista(N,p[i].dr[j]))
N[k++]=p[i].dr[j];
}
N[k]='\0';
}
void terminale(struct productie p[],int n, char T[],char N[])
{
int k=0;
int i,j;
for(i=0;i<n;i++)
{
if(!exista(N,p[i].st) && !exista(T,p[i].st)&& p[i].st!='$')
T[k++]=p[i].st;
for(j=0;j<strlen(p[i].dr);j++)
if(!exista(N,p[i].dr[j]) && !exista(T,p[i].dr[j]) && p[i].dr[j]!='$')
T[k++] = p[i].dr[j];
}
T[k]='\0';
}
void afisare(FILE *g,int n){
int i;
for(i=0;i<n;i++)
fprintf(g,"%c -> %s",prod[i].st, prod[i].dr);
}
int main(){
char N[30];
char T[30];
int i,j,n;
f=fopen("in.txt","r");
n=citire(f);
neterminale(prod,n,N);
printf("\nNeterminalele sunt:{");
for(i=0;i<strlen(N);i++)
printf("%c ",N[i]);
printf("}");
terminale(prod,n,T,N);
printf("\nTerminalele sunt:{");
for(i=0;i<strlen(T);i++)
{
printf(" %c ",T[i]);
}
printf("}");
printf(prod[0].dr[0]); //AT THIS LINE IS THE PROBLEM
g=fopen("out.txt","w");
afisare(g,n);
fclose(f);
fclose(g);
return 0;
}
The in.txt :
S->aS
S->a
S->$
S->AB
A->b
B->c
If you want to print a string directly, you need to provide a pointer to the first character, not the first character itself:
printf(&(prod[0].dr[0]));
or
printf(prod[0].dr);
But you can't limit this to a single char (there is no nprintf function).

C programming, solving C2073 error in structures?

So, I'm new in this, and trying structures out... Error C2073 appeares..can someone help and give some advice?
I tried with FOR in main function to call functions "ispis" which is for printf only, and function "unos" which is for scanf so many times, how big is int "broj_knjiga". I tried to work with -> instead of . but I simply can't solve this problem(which is simple). Someone help?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void ispis(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\ID knjige: %d", &pt[i].ID_knjige);
printf("\Autor knjige: %s", &pt[i].autor);
printf("\Naslov knjige: %d", &pt[i].naslov);
}
}
void unos(struct knjiga *pt, int broj)
{
int i;
for(i=0; i<broj; i++)
{
printf("\nUnesite ID knjige: ");
scanf("%d", &pt[i].ID_knjige);
printf("\nUnesite autora knjige: ");
scanf("%d", &pt[i].autor);
printf("\nUnesite naslov knjige: ");
scanf("%d", &pt[i].naslov);
}
}
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
int main()
{
struct knjiga *pt;
int broj_knjiga;
printf("Unesite koliko knjiga unosite: ");
scanf("%d", &broj_knjiga);
pt=(struct knjiga*)malloc(sizeof(struct knjiga)*broj_knjiga);
unos(pt, broj_knjiga);
ispis(pt, broj_knjiga);
return 0;
}
C is compiled from the top-down. Structs only exist below their declarations. Moving your struct definition above the rest of your code will fix your problem.
knjiga myStruct1; // invalid
struct knjiga
{
int ID_knjige;
char autor[40];
char naslov[20];
};
knjiga myStruct2; // valid

clrscr error in main program debug

I am receiving the error in function main for my clrscr(); but I thought I had to clear when using fflush(stdin);?
I feel like I am missing something simple here but if anyone can shed some like I would appreciate it!
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int age;
};
typedef struct person NAME;
NAME stud[10], temp[10];
void main()
{
int no,i;
void sort(int N); /* Function declaration */
clrscr();
fflush(stdin);
printf("Enter the number of students in the list\n");
scanf("%d",&no);
for(i = 0; i < no; i++)
{
printf("\nEnter the name of person %d : ", i+1);
fflush(stdin);
gets(stud[i].name);
printf("Enter the age of %d : ", i+1);
scanf("%d",&stud[i].age);
temp[i] = stud[i];
}
printf("\n*****************************\n");
printf (" Names before sorting \n");
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",temp[i].name,temp[i].age);
}
sort(no); /* Function call */
printf("\n*****************************\n");
printf (" Names after sorting \n");
printf("\n*****************************\n");
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",stud[i].name,stud[i].age);
}
printf("\n*****************************\n");
} /* End of main() */
/* Function to sort the given names */
void sort(int N)
{
int i,j;
NAME temp;
for(i = 0; i < N-1;i++)
{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
} /* end of sort() */
Put the function prototype void sort(int N); outside main()
You don't have (but you may) execute clrscr() before fflush(stdin). In this case contents of your screen (which you want to clear) have nothing to do with stdin.
You can read more about fflush() and the motivation to use it, here.
I assume that you get a compilation error. It is caused by the line above the one where you see the error.
As suggested by #Catalyst, it is caused here by the line
void sort(int N); /* Function declaration */`
because C does not allow functions to be declared locally inside other functions (and main is a function).
You can simply fix it that way :
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int age;
};
typedef struct person NAME;
NAME stud[10], temp[10];
void sort(int N); /* Function declaration */
int main() // void main is incorrect
{
int no,i;
clrscr();
fflush(stdin);
...
Note also the int main() instead of void main(). It is harmless on Windows, but is still incorrect.

Resources