problem with fseek()

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 1033 Views - Last Post: 24 July 2011 - 09:34 AM Rate Topic: -----

#1 rahul172  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 15-July 11

problem with fseek()

Posted 15 July 2011 - 01:01 AM

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

int main(int argc,char *argv[])
{
	FILE *fp;

	if(argc!=3){
			printf("usage: SEEK filename byte\n");
			exit(1);
			}

	if((fp=fopen("argv[1]","r+"))==NULL){
			printf("cannot open file.\n");
			exit(1);
			}

	if(fseek(fp,atol(argv[2]),SEEK_SET)){
			printf("seek error\n");
			exit(1);
			}

	printf("Byte at %ld is %c.\n",atol(argv[2]),getc(fp));
	fclose(fp);

	return 0;
}




i tried to run this program with cmd like this...
F:\><filename> rahul 24L
but it returns "cannot open file"...i can't find any errrs...it is not giving fseek() output.

This post has been edited by stayscrisp: 15 July 2011 - 01:55 AM
Reason for edit:: please use code tags!


Is This A Good Question/Topic? 0
  • +

Replies To: problem with fseek()

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: problem with fseek()

Posted 15 July 2011 - 01:21 AM

Run it like this:
rahul <filename> <position>

Was This Post Helpful? 0
  • +
  • -

#3 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: problem with fseek()

Posted 15 July 2011 - 02:21 AM

Remove the double quotation from argv[1] here:
if((fp=fopen("argv[1]","r+"))==NULL)

Was This Post Helpful? 3
  • +
  • -

#4 rahul172  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 15-July 11

Re: problem with fseek()

Posted 15 July 2011 - 07:09 AM

View PostPlasticineGuy, on 15 July 2011 - 01:21 AM, said:

Run it like this:
rahul <filename> <position>


this method gives the error....
rahul is not recognized as an external or internal command
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: problem with fseek()

Posted 15 July 2011 - 07:19 AM

Umm...substitute the name of YOUR EXECUTABLE for "rahul" in that example.
Was This Post Helpful? 0
  • +
  • -

#6 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: problem with fseek()

Posted 15 July 2011 - 07:47 AM

If the file you are trying to read is not in the current directory then you have to specify the filepath in the command prompt.Suppose rahul.txt is the file you are trying to read and it is in C:\ drive then your command should be like this:

F:\><Executable filename> c:\\rahul.txt 24L


Don't forget the extension.

This post has been edited by aaa111: 15 July 2011 - 07:56 AM

Was This Post Helpful? 1
  • +
  • -

#7 rahul172  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 15-July 11

Re: problem with fseek()

Posted 15 July 2011 - 01:33 PM

View Postaaa111, on 15 July 2011 - 07:47 AM, said:

If the file you are trying to read is not in the current directory then you have to specify the filepath in the command prompt.Suppose rahul.txt is the file you are trying to read and it is in C:\ drive then your command should be like this:

F:\><Executable filename> c:\\rahul.txt 24L


Don't forget the extension.

thnx for your reply....but i am getting the same output....."cannot open file".
noname11.cpp is the file name.
i execute it from command prompt like this
noname11 rahul 24L
i think there is some problem in 24L.
Was This Post Helpful? 0
  • +
  • -

#8 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: problem with fseek()

Posted 15 July 2011 - 01:54 PM

Specify both the filepath (where the file is in for example c:\,d:\ etc) and extension(.txt,.dat etc) for your second command such as this:

noname11 c:\\rahul.txt 24L


Assuming rahul is in C:\ drive and the extension is .txt.

This post has been edited by aaa111: 15 July 2011 - 02:00 PM

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: problem with fseek()

Posted 15 July 2011 - 02:20 PM

If you are passing the file name as a command line argument you do not need the double backslash '\\' a single back slash should work. You only need the double backslashes in a string literal inside a program. Also if your path has any spaces you need to enclose the path with quotation marks.
"C:\YOUR DIRECTORY\YourFilename.YourExtension" Also note if you get confused about when to use the double backslashes you can also use a single forward slash in your paths: "C:/directory/filename.ext", this method will work on both Windows and Linux.

Jim

This post has been edited by jimblumberg: 15 July 2011 - 02:22 PM

Was This Post Helpful? 1
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: problem with fseek()

Posted 15 July 2011 - 07:05 PM

You can't execute a cpp file, it has to be the executable your compiler generated.
Was This Post Helpful? 0
  • +
  • -

#11 rahul172  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 15-July 11

Re: problem with fseek()

Posted 16 July 2011 - 07:26 AM

View Postaaa111, on 15 July 2011 - 01:54 PM, said:

Specify both the filepath (where the file is in for example c:\,d:\ etc) and extension(.txt,.dat etc) for your second command such as this:

noname11 c:\\rahul.txt 24L


Assuming rahul is in C:\ drive and the extension is .txt.

thnx for your attention...but its still giving the same result....i think that i m missing something in this program to redirect the control to fseek();
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: problem with fseek()

Posted 16 July 2011 - 08:14 AM

Why don't you post your current code, along with the command line you're using to run the program and a listing of the directory in which you're running the program?
Was This Post Helpful? 0
  • +
  • -

#13 rahul172  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 9
  • Joined: 15-July 11

Re: problem with fseek()

Posted 17 July 2011 - 11:56 AM

View PostJackOfAllTrades, on 16 July 2011 - 08:14 AM, said:

Why don't you post your current code, along with the command line you're using to run the program and a listing of the directory in which you're running the program?
I have already posted my current coe and thats it.
i've saved the file as noname11.cpp in c:\ and the file it opens is named as rahul in F:\.
so i change the directory to f:\ in cmd and then execute the program as "noname11 rahul 24L". i am providing the screen prints of these two....
cmd -->Attached Image
output-->Attached Image
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: problem with fseek()

Posted 17 July 2011 - 12:25 PM

If you have not made any of the changes that were suggested in earlier posts then you will definitely not be able to open the file. The following line:
  if((fp=fopen("argv[1]","r+"))==NULL){

you need to remove the quotation marks from around argv[1].

Jim
Was This Post Helpful? 1
  • +
  • -

#15 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: problem with fseek()

Posted 17 July 2011 - 12:47 PM

Quote

I have already posted my current coe and thats it.

Why do you ask for help and then ignore the advice you're given? That's not very bright.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2