Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

i want to read from file and assing to variable

Posted on 2003-02-19
5
Medium Priority
?
357 Views
Last Modified: 2010-04-15
Hi all,

         I want to read from file and assing to variable in int format. For that i wrote bellow mentioned code.

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

int main(void)
{
clrscr();
   FILE *stream;
   char string[] = "Th";
   char *ch="10";
  int  p=0;
  stream = fopen("DUMMY.TXT", "r");
   fseek(stream, 0, SEEK_SET);
    fgets(ch, strlen(string), stream);
     p=atoi(ch);
     cout<<p;
   fclose(stream);
   getch();
   return 0;
}

it's working fine individually. but when i put it in another file out of which i am making dll then when i am
running it it gives sharing acess violation in fgets function.

       Can any one help me is there any other logic when making dll of it?

Thanks in advace,

Regards,
Pralay
0
Comment
Question by:pralaydesai
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
Kryp earned 150 total points
ID: 7980960
#include <stdio.h> ANSI-C I/O
#include <conio.h> DOS I/O
#include <iostream.h> ANSI-C++ I/O
Make your mind up!

> stream = fopen("DUMMY.TXT", "r");
Follow this with
if ( stream == NULL ) {
    perror( "Unable to open file" );
    return;  /* or some other error action */
}

> fseek(stream, 0, SEEK_SET);
Kinda reduntant, since and "r" opened file starts at the beginning anyway


  char string[] = "Th";
  char *ch="10";
string isn't used (except for its length), and the memory which ch points to can't be modified (this is your access violation).

Use this
char buff[BUFSIZ];
if ( fgets( buff, BUFSIZ, stream ) != NULL ) {
    p = atoi( buff );
}

Though using strtol() in place of atoi() would permit better error checking

0
 
LVL 1

Expert Comment

by:Gula
ID: 7983927
pralaydesai, you have asked 14 questions, and have accepted none of them ... If you got your solutions ,  go through your questions and accept the answers that give solution to your question, othervise they remain in the system as unsolved
thank you
0
 

Author Comment

by:pralaydesai
ID: 7992768
hi,
 
   It work bit not fully.

          i know i have to say it works and accept the answer but if it works fully then is it wrong?

             One more request,

                  I want to read a specific lin's specific character is it possible?

Regards,
Pralay
0
 
LVL 2

Expert Comment

by:Kryp
ID: 8002155
I think I will decline to answer until you've addressed the issue raised by Gula.

> It work bit not fully.
And my psychic ability tells me exactly what you did wrong.
Post your latest code and new question!

There's no point you offering points if you've no intention of completing the process.

> i know i have to say it works and accept the answer but if it works fully then is it wrong?
If the answer helps you past whatever problem you have then it's an acceptable answer in my opinion.
0
 

Author Comment

by:pralaydesai
ID: 8006436
Thanks ,

          It took bit time for understand what you post. Initially what i thought was in wrong direction.

Regards,
Pralay
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question