?
Solved

i want to read from file and assing to variable

Posted on 2003-02-19
5
Medium Priority
?
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Suggested Courses

764 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