473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with file opening by C

Hello , I'm new on programming and I'm trying to pass one more step to files. I wrote the following code and opened the " dosya.txt " file to true folder. At first I wrote it to G:\\dosya.txt , then I got null character as a return. After this I changed it to C:\\dosya.txt , now I got
" Unhandled exception at 0x5521e42e (msvcr100d.dll) in asd.c.exe: 0xC0000005: Access violation writing location 0xcccccccc. " error and I don't know how to solve it. I think I didn't do anything wrong but my code doesn't work. Can you help me to solve this ? Thanks a lot :)

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {
  5.     int i,a[5];
  6.     FILE *ptr;
  7.     ptr = fopen("C:\\dosya.txt","r");
  8.     if(ptr==NULL)
  9.         printf("File does not exist !");
  10.     for(i=0;i<5;i++,ptr++)
  11.     {
  12.         fscanf(ptr,"%d",a[i]);
  13.         printf("%d\n",a[i]);
  14.     }
  15. }
Apr 22 '11 #1

✓ answered by Alex Bránya

I have compiled after modification, anyway try this:
(I have compiled too, and it runs)


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.   int main()
  4.   {
  5.        int i;
  6.        int a[]={19,21,9,3,75};  //initialize array
  7.        FILE *ptr;
  8.        ptr = fopen("C:\\dosya.txt","w");
  9.        if(ptr==NULL)
  10.        printf("File does not exist !");
  11.  
  12.  
  13.        for(i=0;i<5;i++) 
  14.        {           
  15.        fprintf(ptr,"%d",a[i]); //write to file
  16.        }
  17.  
  18.     rewind (ptr);  //reset file pointer
  19.  
  20.        for(i=0;i<5;i++) 
  21.        {
  22.            fscanf(ptr,"%d",a[i]); //read from file
  23.            printf("%d\n",a[i]);   
  24.        }
  25.  
  26.          fclose (ptr);
  27.  
  28.  }
  29.  

3 3209
Here is an answer:


Expand|Select|Wrap|Line Numbers
  1. //don't increase ptr manually, it's done by internal
  2. for(i=0;i<5;i++,ptr++)
  3.  
  4. //this is the right format
  5. for(i=0;i<5;i++)
  6.  
Apr 22 '11 #2
I just done what you've said but it still doesn't work. I got same error message :(
Apr 22 '11 #3
I have compiled after modification, anyway try this:
(I have compiled too, and it runs)


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.   int main()
  4.   {
  5.        int i;
  6.        int a[]={19,21,9,3,75};  //initialize array
  7.        FILE *ptr;
  8.        ptr = fopen("C:\\dosya.txt","w");
  9.        if(ptr==NULL)
  10.        printf("File does not exist !");
  11.  
  12.  
  13.        for(i=0;i<5;i++) 
  14.        {           
  15.        fprintf(ptr,"%d",a[i]); //write to file
  16.        }
  17.  
  18.     rewind (ptr);  //reset file pointer
  19.  
  20.        for(i=0;i<5;i++) 
  21.        {
  22.            fscanf(ptr,"%d",a[i]); //read from file
  23.            printf("%d\n",a[i]);   
  24.        }
  25.  
  26.          fclose (ptr);
  27.  
  28.  }
  29.  
Apr 22 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: ramsin.savra | last post by:
Hi, I'm using same code for opening a TIFF file in VC++ works just fine but under linux it says unable to open the tiff file! Any comment on this? if (argc < 2) { cout << "Not enough...
11
by: Lee Crabtree | last post by:
I've got a solution that has a C++ project that compiles to a DLL, and a C# project that is the main piece of the program. The DLL is managed C++ on top of regular C++. In one of the native...
1
by: aham | last post by:
Hi everyone, I wrote code for authentication, after a user enters userid and password, it should open a file, which will be taken from a database. Code is taking he file name properly, but it is...
3
by: siricharan82 | last post by:
Hi Everybody. I have a problem on .NET while opening. Actually before that it works normally.But one day it does not opening. I dont know the reason why it is happend like that. Even I am...
4
by: amit.atray | last post by:
Hi, I am trying to open an ISAM file with isopen call. But I am getting error with iserror set to 111. I tried to check 111 error code ... 111 ENOREC Action: No record could be found...
1
by: ranjukbs | last post by:
Hi All, I am facing problem with opening a external url from menu. If i link it directly with "www" url its appening relative path along with the external url. I want this to open in same window....
2
by: Wayne | last post by:
I'm having a problem that I've seen before on XP and am now seeing on Vista. It started yesterday and I can't associate it with anything that I've done to the PC, like installing software etc. I...
1
by: Sanjeeva K kanakam | last post by:
Hi all, I am developing one application. In that i have created on report file which contains tables, titles and tree view list also by using HTML tags. I am opening this file in my application it...
1
by: manu | last post by:
Hi, Can anyone please tell me the difference of opening file in Binary mode or in Text mode?
3
by: premMS143 | last post by:
Dear all, In our company, we are using a system having windows XP & Office 2007 installed. Since from last 1 week we are facing problem with opening office files. All office files icons are changed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.