473,769 Members | 6,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can't read file?

the following code is used to read a file called "input.txt"
# include <stdio.h>
int main(int argc,char *argv[]){
FILE *fp;
int ch;
fp=fopen("input .txt","r");
ch=fscanf(fp,"% d",&ch);
printf("%d\n",c h);

fclose(fp);
return 0;
}

the contains in "input.txt"
3
3 14
2 8
-1 0
when i run the program the output is:
1

what's going on?
why the output is not 3?

Nick
Nov 15 '05 #1
11 1443
nick <i1********@yah oo.com> writes:
the following code is used to read a file called "input.txt"
# include <stdio.h>
int main(int argc,char *argv[]){
FILE *fp;
int ch;
fp=fopen("input .txt","r");
ch=fscanf(fp,"% d",&ch);
printf("%d\n",c h);

fclose(fp);
return 0;
}

the contains in "input.txt"
3
3 14
2 8
-1 0
when i run the program the output is:
1

what's going on?
why the output is not 3?


fscanf() returns the number of items scanned, which in this case will
be 1 if it succeeds. You're passing &ch as an argument to fcanf()
*and* you're assigning the result to ch. Don't do that.

(I'm not certain whether it invokes undefined behavior, but it's
certainly not what you want to do .)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #2
ch=fscanf(fp,"% d",&ch);
this line is the answer! because fscanf() return a int number 1,your ch
got it.
the true way was like this
fscanf(fp,"%d", &ch);
write like this is all ok,fscanf set value to ch,and you will found
the result is 3

best wishes

usr_root

Nov 15 '05 #3
thanks!
Nov 15 '05 #4
"us******@gmail .com" <us******@gmail .com> writes:
ch=fscanf(fp,"% d",&ch);
this line is the answer! because fscanf() return a int number 1,your ch
got it.
the true way was like this
fscanf(fp,"%d", &ch);
write like this is all ok,fscanf set value to ch,and you will found
the result is 3


I think we've told you this before. Please pay attention this time.

You need to provide some context when you post a followup. Not
everyine has easy access to the parent article. It may not have
arrived yet, it may have expired, or somebody's newsreader might not
have a command to dispaly it.

If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

And ask Google to fix their broken interface.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5

"nick" <i1********@yah oo.com> wrote in message
news:dg******** ***@justice.its c.cuhk.edu.hk.. .
the following code is used to read a file called "input.txt"
# include <stdio.h>
int main(int argc,char *argv[]){
FILE *fp;
int ch;
fp=fopen("input .txt","r");
ch=fscanf(fp,"% d",&ch);
printf("%d\n",c h);

fclose(fp);
return 0;
}

the contains in "input.txt"
3
3 14
2 8
-1 0
when i run the program the output is:
1

what's going on?
why the output is not 3?


Look in your textbook. What does 'fscanf()' return?

-Mike
Nov 15 '05 #6

Keith Thompson 写道:
"us******@gmail .com" <us******@gmail .com> writes:
ch=fscanf(fp,"% d",&ch);
this line is the answer! because fscanf() return a int number 1,your ch
got it.
the true way was like this
fscanf(fp,"%d", &ch);
write like this is all ok,fscanf set value to ch,and you will found
the result is 3


I think we've told you this before. Please pay attention this time.

You need to provide some context when you post a followup. Not
everyine has easy access to the parent article. It may not have
arrived yet, it may have expired, or somebody's newsreader might not
have a command to dispaly it.

If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

And ask Google to fix their broken interface.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Thank you!i get it!

Nov 15 '05 #7
On Fri, 23 Sep 2005 01:15:33 GMT, Keith Thompson <ks***@mib.or g> wrote
in comp.lang.c:
nick <i1********@yah oo.com> writes:
the following code is used to read a file called "input.txt"
# include <stdio.h>
int main(int argc,char *argv[]){
FILE *fp;
int ch;
fp=fopen("input .txt","r");
ch=fscanf(fp,"% d",&ch);
printf("%d\n",c h);

fclose(fp);
return 0;
}

the contains in "input.txt"
3
3 14
2 8
-1 0
when i run the program the output is:
1

what's going on?
why the output is not 3?


fscanf() returns the number of items scanned, which in this case will
be 1 if it succeeds. You're passing &ch as an argument to fcanf()
*and* you're assigning the result to ch. Don't do that.

(I'm not certain whether it invokes undefined behavior, but it's
certainly not what you want to do .)


Can't have undefined behavior, 'ch' in modified twice, but there are
at least two sequence points between the assignments. The assignment
to 'ch' via the pointer passed to fscanf() must take place and
complete at a sequence point before the statement returning the value,
which itself is terminated by another sequence point.

And the assignment of the return value to 'ch' cannot take place until
after that return statement in fscanf().

No different than:

#include <stdio.h>
int int_3(int *ip)
{
*ip = 3; /* sequence point #1 */
return 1; /* sequence point #2 */
}
int main(void)
{
int i;
i = int_3(&i);
printf("%d\n", i);
return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #8
Jack Klein <ja*******@spam cop.net> writes:
On Fri, 23 Sep 2005 01:15:33 GMT, Keith Thompson <ks***@mib.or g> wrote
in comp.lang.c:
nick <i1********@yah oo.com> writes:
> the following code is used to read a file called "input.txt"
> # include <stdio.h>
> int main(int argc,char *argv[]){
> FILE *fp;
> int ch;
> fp=fopen("input .txt","r");
> ch=fscanf(fp,"% d",&ch);
> printf("%d\n",c h);
>
> fclose(fp);
> return 0;
> }
[...] fscanf() returns the number of items scanned, which in this case will
be 1 if it succeeds. You're passing &ch as an argument to fcanf()
*and* you're assigning the result to ch. Don't do that.

(I'm not certain whether it invokes undefined behavior, but it's
certainly not what you want to do .)


Can't have undefined behavior, 'ch' in modified twice, but there are
at least two sequence points between the assignments. The assignment
to 'ch' via the pointer passed to fscanf() must take place and
complete at a sequence point before the statement returning the value,
which itself is terminated by another sequence point.

And the assignment of the return value to 'ch' cannot take place until
after that return statement in fscanf().


Ok -- but doesn't that assume that fscanf() is implemented in C?

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #9
On Fri, 23 Sep 2005 07:45:05 GMT, Keith Thompson <ks***@mib.or g> wrote
in comp.lang.c:
Jack Klein <ja*******@spam cop.net> writes:
On Fri, 23 Sep 2005 01:15:33 GMT, Keith Thompson <ks***@mib.or g> wrote
in comp.lang.c:
nick <i1********@yah oo.com> writes:
> the following code is used to read a file called "input.txt"
> # include <stdio.h>
> int main(int argc,char *argv[]){
> FILE *fp;
> int ch;
> fp=fopen("input .txt","r");
> ch=fscanf(fp,"% d",&ch);
> printf("%d\n",c h);
>
> fclose(fp);
> return 0;
> } [...] fscanf() returns the number of items scanned, which in this case will
be 1 if it succeeds. You're passing &ch as an argument to fcanf()
*and* you're assigning the result to ch. Don't do that.

(I'm not certain whether it invokes undefined behavior, but it's
certainly not what you want to do .)


Can't have undefined behavior, 'ch' in modified twice, but there are
at least two sequence points between the assignments. The assignment
to 'ch' via the pointer passed to fscanf() must take place and
complete at a sequence point before the statement returning the value,
which itself is terminated by another sequence point.

And the assignment of the return value to 'ch' cannot take place until
after that return statement in fscanf().


Ok -- but doesn't that assume that fscanf() is implemented in C?


No, the C library does not need to be written in C, but it is defined
and prototyped in terms of C functions. No matter how it is actually
implemented, it must behave "as if" it was written in C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3410
by: Sven Groot | last post by:
Using Outlook 2002 NL with all updates installed. I'm trying to open a backup pst file I have on CD. I have copied the file to my harddrive, made sure it's not read-only, am logged in as adminstrator and have full control over the file, and so has the SYSTEM account. The error message I get is: "backup.pst is not a personal folder file" (I translated this from the Dutch text "backup.pst is geen bestand met persoonlijke mappen" so the...
0
2104
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape. Once the compare is complete the file that was copied to a temp location needs to be deleted. I am using the method file.copy(sourcePath, tempPath, true) to copy the file and then file.delete(tempPath) to delete the file. On some of the files...
10
2288
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am supposed to change the buffer size and see how this affects the read time. In other words, the buffer is supposed to limit how much of the file gets read per call, and cause some change in speed. I am supposed to do the same with fread as well, but...
1
3509
by: Martin | last post by:
I use dbx and i got the following error: Reading GL_CliConnMgr core file header read successfully Reading ld.so.1 dbx: core file read error: address 0xff3e6000 not available dbx: core file read error: address 0xff3e70a0 not available dbx: warning: could not initialize librtld_db.so.1 -- trying libDP_rtld_db.so Make sure this is the same version of Solaris where the core dump
2
4347
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit this sound (file) on a Windows socket (not using FTP but TCP), can I read the file into a byte array? Because if it is possible then I can send it on the socket but the problems I foresee are: How will I repack it into the file. Will just reading...
4
2747
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void Main()
18
4350
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
8
23907
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
2
2757
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include <string>
5
3305
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using namespace std; int read(string filename, int last_pos) {
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9997
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8873
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.