Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading a file 1

Status
Not open for further replies.

leela74

Programmer
May 11, 2001
26
IN
Hi,

I have a problem. There is one file called abc in that list of ids and names are there. for ex.
1234 sridhar
5678 abcd
etc
I want write a c program to validate the entered number with the file nuumber and get the name and open a file with extension(suppose entered number is 1234 a file shouldbe opened with sridhar.txt)....

How do I do this in c...

Thanks in advance

Sridhar
 
So here goes the logic ...
Read the file into array of structure, using fscanf.
now compare the number entered by the user with the number present in structure array. When the number is found use the string part of that array to create a variable which will concatenate the value of string with ".txt" string nd now using a simple fopen u can open the file.

Refer to these threads for better explanation.
thread205-78931
thread205-80343

I hope that u were not asking for source code. If it is so just post the req. I'll respond it. However if it is not extremely urgent try it out on your own this will help u to understand C better.


Regards,
SwapSawe.
 
Hi, SwapSawe

Thanks for the reply. I am trying for the last 3 days with the same logic u explained in the mail.

If u don't mind can i get the c code of my question. To recap I am explaining again.

Suppose user enters a number 1234. In the array pertaining to user 1234 there will a name(for eg.sridhar) I want to test for number is present in the array if present with that name I want to open a file (for eg. sridhar.txt). Can u please help with the code as it is bit urgent and I am not in aposition to expermiment.

Thanks in advance

Sridhar
 
int main()
{
struct dtls{
int no;
char name[20];
}*data;
int i=0,reccount,searchcode;
FILE *fp,*fs;
char *filename;
fp=fopen("abc","r");
while(!feof(fp))
{
//Write malloc for allocating memory.
fscanf("%d %s",&(data+i)->no,(data+i)->name);
i++;
}
fclose(fp);
reccount=i;
printf("Enter code :");
scanf("%d",&searchcode);
i=0;
while(i<reccount)
{
if(searchcode==(data+i)->no)
{
found='y';
break;
}
i++;
}
if(found=='n')
{
exit(1);
}
strcpy(filename,(data+i)->name);
strcat(filename,&quot;.txt&quot;);
//If the file is to be created
fs=fopen(filename,&quot;w&quot;);
fprintf(fp,,&quot;%s&quot;,&quot;Hello&quot;)
fclose(fs);
return 1;
}


This prog will open the file. I am at work so I have not tried it out. May b u get some direct errors like var not declared and even I've not checked the existence or file operability. So check 4 these direct error else the code is fine.
Regards,
SwapSawe.
 
Hi again, SwapSawe

Thanks for the immediate reply though u r busy at work and sparing time to take some trouble.

I need some more help from u...

There are is one compilation error which I am unable to get it I am pasting it here....

error C2664: 'fscanf' : cannot convert parameter 1 from 'char [6]' to 'struct _iobuf *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

I couldn't undertsand the error.

Thanks in advance

Sridhar
 
If u r talking bout the code I've posted. the error is in
fscanf.
It should be-
fscanf(fp,&quot;%s %d&quot;,&(data+i)->no,(data+i)->name);


If otherwise pls. paste the code thats giving u the error.

Regards,
SwapSawe
 
Hi again SwapSawe

I kept fp at fscanf it has compiled successfully and when I am running the program it is giving the following error message...

The instruction at 0x00405eb8 referenced memory at 0xccccccc the memory could not be read.
Click OK to terminate the application
Click CANCEL to debug the application

When I click the cancel it is showing the registry...

The following is the code...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
struct dtls{
int no;
char name[20];
}*data;
int i=0,reccount,searchcode;
FILE *fp,*fs;
char *filename;
char found;
fp=fopen(&quot;abc&quot;,&quot;r&quot;);
while(!feof(fp))
{
//Write malloc for allocating memory.
fscanf(fp,&quot;%d %s&quot;,&(data+i)->no,(data+i)->name);
i++;
}
fclose(fp);
reccount=i;
printf(&quot;Enter code :&quot;);
scanf(&quot;%d&quot;,&searchcode);
i=0;
while(i<reccount)
{
if(searchcode==(data+i)->no)
{
found='y';
break;
}
i++;
}
if(found=='n')
{
exit(1);
}
strcpy(filename,(data+i)->name);
strcat(filename,&quot;.txt&quot;);
//If the file is to be created
fs=fopen(filename,&quot;w&quot;);
fprintf(fp,&quot;%s&quot;,&quot;Hello&quot;);
fclose(fs);
return 1;
}

 
The problem is with memory allocation.
Check that.
SwapSawe.
 
I saw the prog and made the req. changes. Pls note them

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
struct dtls{
int no;
char name[20];
}data[5];
int i=0,reccount,searchcode;
FILE *fp,*fs;
char *filename;
char found;
fp=fopen(&quot;\\SwapC\\Querries\\abc.txt&quot;,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;No such file&quot;);
exit(1);
}
//data = (struct dtls *) malloc(sizeof(struct dtls)*5);

while(!feof(fp))
{
fscanf(fp,&quot;%d %s&quot;,&data.no,data.name);
i++;
}
fclose(fp);
reccount=i;
printf(&quot;Enter code :&quot;);
scanf(&quot;%d&quot;,&searchcode);
i=0;
while(i<reccount)
{
if(searchcode==data.no)
{
found='y';
break;
}
i++;
}
if(found=='n')
{
exit(1);
}
strcpy(filename,data.name);
strcat(filename,&quot;.txt&quot;);
//If the file is to be created
fs=fopen(filename,&quot;w&quot;);
if(fs==NULL)
{
printf(&quot;Cannot create file&quot;);
exit(1);
}
fprintf(fs,&quot;%s&quot;,&quot;Hello&quot;);
fclose(fs);
return 1;
}

Now the code works fine.
Regards,
SwapSawe.
 
Hi, SwapSawe

Thanks alot for the changed code. The code is working fine and asking the code to be entered. When I enter the code I am getting the earlier message...The memory instruction can not be read...
The instruction at 0x00405eb8 referenced memory at 0xccccccc the memory could not be read.
Click OK to terminate the application
Click CANCEL to debug the application

I couldn't understand why this error is coming. I have added #include <dos.h>. I am using VC++6.0 compiler.

Thanks in advance

Sridhar
 
Hi, SwapSwae

Y'day I tested the code in Turbo c++ 3.0 compiler and the error I am getting is &quot;Null Pointer exception&quot;. Then I changed the code and brought all the contents after the If found == 'n' to found == 'y' and now I am able to create a file with the available name.
But still I am getting the null pointer exception.

There are 2 doubts regarding the above code...

1. Is there any way to avoid this Null pointer exception

2. Is there any possibility to add date and time(ddmm&HHMM) to file name and create a file.

Thanks In advance

Sridhar
 
No need to change the code. Initialize the found variable with n. ie char found = 'f';

The reason for the null pointer execption is the use of filename without any memory allocation.

before the strcpy statement put

filename = (char *) malloc( sizeof(char) * (strlen(data.name) + 5) );
and before the return 1 statement put free(filename);

otherwise, make the filename as a character array.
like char filename[20].




 
Hi, SwapSwae

It is working fine after changing the code.

Is there any possibility to change the file name with some more additions... like in the file name I have to add date and time suppose for eg.
abc is the file name I want to adddate and time in the format ddmm and HHMM then the file name becomes abc15051100

Thanks in advance

Sridhar
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
struct dtls{
int no;
char name[20];
}data[5];
int i=0,reccount,searchcode;
FILE *fp,*fs;
char *filename=&quot;&quot;;
char found;
fp=fopen(&quot;\\SwapBkUp\\abc.txt&quot;,&quot;r&quot;);
if(fp==NULL)
{
printf(&quot;No such file&quot;);
exit(1);
}
while(!feof(fp))
{
fscanf(fp,&quot;%d %s&quot;,&data.no,data.name);
i++;
}
fclose(fp);
reccount=i;
printf(&quot;Enter code :&quot;);
scanf(&quot;%d&quot;,&searchcode);
i=0;
while(i<reccount)
{
if(searchcode==data.no)
{
found='y';
break;
}
i++;
}
if(found=='n')
{
exit(1);
}
filename = (char *) (malloc(sizeof(10)));
strcpy(filename,data.name);
strcat(filename,&quot;.txt&quot;);
//If the file is to be created
fs=fopen(filename,&quot;w&quot;);
if(fs==NULL)
{
printf(&quot;Cannot create file&quot;);
exit(1);
}
fprintf(fs,&quot;%s&quot;,&quot;Hello&quot;);
fclose(fs);
return 1;
}

I have compiled it in VC++ 6.0 now it will work.
Regards,
SwapSawe.
 
Hi Sridhar,

i m sorry I had been very fast at reading ur questions and so I skipped ur query for date and time additions X-). Here is the code by which u can get date and time in string use string functions to add the desired values to your filenames.

#include <time.h>
#include <stdio.h>
void main(void)
{
char datebuf[9];
char timebuf[9];

_strdate(datebuf);
_strtime(timebuf);
printf(&quot;Date: %s Time: %s\n&quot;,datebuf,timebuf);
}


Hope it helps,
Regards,
SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top