Register to reply

C programming question

by stellina
Tags: programming
Share this thread:
stellina
#1
Jun9-11, 04:17 PM
P: 8
Hello! I am learning how to use file and argc and I am stuck on this assignment : write a program that sequentially displays on screen the contents of all of the files listed in the command line. Use argc to control a loop.

Here is my code, I'll try to enclose it in [ CODE ] [ /CODE ] tags if it doesn't work please explain how to do it:

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

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

int i;
FILE *fp;

for(i = 0; i < argc; i++)

fp = fopen(argv[i],"r");

fprintf(fp, "Testing...\n");

return 0;
}
]



I can't check if it works because I don't have any file in the command line and I know too little about files, the book doesn't help much. Thanks
Phys.Org News Partner Science news on Phys.org
Scientists discover RNA modifications in some unexpected places
Scientists discover tropical tree microbiome in Panama
'Squid skin' metamaterials project yields vivid color display
Timo
#2
Jun9-11, 04:37 PM
P: 324
The code tags work fine for me:
#include <stdio.h>
#include <stdlib.h>

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

int i;
FILE *fp;

for(i = 0; i < argc; i++)

fp = fopen(argv[i],"r");

fprintf(fp, "Testing...\n");

return 0;
}
Without knowing much of the old C commands or what your program is supposed to do (the "Testing..." reads as if you don't even expect this program to do what it is supposed to): two comments:
1) Which command(s) are inside the for(i=...) loop? Which are outside?
2) I think that argv[0] is the name of the program that is being executed, not the first parameter passed. I may be wrong there, but you may want to check that.
MisterX
#3
Jun9-11, 04:42 PM
P: 583
argv[0] is for the program name. Your program should use file names starting with argv[1]. The line

fprintf(fp, "Testing...\n");
is incorrect - fprintf is for writing to files

You should have a call to fclose within the loop. Otherwise the files will remain open and your program will no longer have the FILE to close them, because it was overwritten with data for the next file.

Mark44
#4
Jun9-11, 05:05 PM
Mentor
P: 21,397
C programming question

I don't advise it, but you could make fprintf work like so:
fprintf(stdout, "Testing...\n");
stdout is the standard output device; i.e., the monitor.

This is functionally the same as
printf("Testing...\n");
stellina
#5
Jun9-11, 05:46 PM
P: 8
Hey guys, thank u all!

So here is what I don't understand:will fopen open the file and show its content?

then there is no use of printf...

About putting fclose in the loop: the problem asks for the contents on the screen to stay displayed. Can anyone explain how does it work?
Mark44
#6
Jun9-11, 05:59 PM
Mentor
P: 21,397
Quote Quote by stellina View Post
Hey guys, thank u all!

So here is what I don't understand:will fopen open the file and show its content?
fopen just opens the file. You need to use an input function such as fscanf to read the contents of each file listed on the command line.
Quote Quote by stellina View Post

then there is no use of printf...

About putting fclose in the loop: the problem asks for the contents on the screen to stay displayed. Can anyone explain how does it work?
After you have read the contents of a given file, close it (with fclose). If you have printed the contents on the screen, they will still be there.
Timo
#7
Jun9-11, 06:02 PM
P: 324
Once you print the content to the screen it will stay there. What you (schematically) do is
- open the file. That is what fopen() does.
- copy the data from the file to the memory (i.e. reading the file content). You still have to figure how to do that.
- copy the data from the memory to the screen (i.e. printing out the content). That is where the printf() comes in.
- close the file.
The content of the file has, ideally, not changed in any of those operations. You do not manipulate the data itself but create copies of it; modifying the file after having printed out the content would not alter the copy on the screen (and closing the file doesn't change its data, anyways).
Grep
#8
Jun9-11, 06:52 PM
P: 296
If all you want to do is dump the file contents to the screen, then you could look at just looping until you get an end of file (EOF) from fgetc. Until you hit the end of the file, you just read a character with fgetc and print it out with fputc (or just putchar). There's probably no need to read the whole file into memory before printing it, anyways.

As Timo points out, make sure you know what's in the for loop and what's not. I would suggest you use curly braces to mark blocks explicitly. There are various reasons, but mostly it's just clearer and helps prevent some common errors. It's optional, but I prefer using curly braces even when there's only one line inside the loop/conditional/etc.

Also don't forget to handle errors. For example, maybe the file doesn't exist. Or maybe you don't have the proper file permissions to read that file.
stellina
#9
Jun9-11, 07:10 PM
P: 8
Thank u so much!!!!

Just one last question. I'd like to check if the program works but I don't know how to create a file in the command line. Any ideas? Thanks!
I like Serena
#10
Jun9-11, 07:20 PM
HW Helper
I like Serena's Avatar
P: 6,189
Hi stellina!

Quote Quote by stellina View Post
Thank u so much!!!!

Just one last question. I'd like to check if the program works but I don't know how to create a file in the command line. Any ideas? Thanks!
Type:

echo "I like stellina" >t.txt

on the command line, and you'll have a file named t.txt. ;)
Mark44
#11
Jun9-11, 07:49 PM
Mentor
P: 21,397
If you create a file as suggested by I like Serena, make sure that you move the file to the same directory as your executable, or your program won't be able to find this file.
jhae2.718
#12
Jun9-11, 07:55 PM
PF Gold
jhae2.718's Avatar
P: 1,160
You can cd to the program's directory first or just include the full path to the directory when running echo to avoid what Mark44 pointed out.
Grep
#13
Jun9-11, 08:24 PM
P: 296
I feel compelled to point out an obvious way to create a more elaborate text file. You can just use an editor. The one you used to make your source file will probably do the job.

For simple things, I often just use the vi editor, but that takes a little getting used to. I assume you're on some kind of Linux system? If you have a gnome desktop, try gedit. For KDE, you can use kate. Both are very capable editors.

So let's say I want to create a text file with gedit, and I'm in the proper directory. I would just type this at the prompt (with whatever filename you want):

gedit textfile.txt

Replace 'gedit' with 'kate' to use that editor instead.
stellina
#14
Jun10-11, 08:47 AM
P: 8
Hey guys, thanks a lot but I am still stuck because I am using xcode as a compiler and there is a different procedure to create a file. I can't figure it out! Is there anyone using xcode that can guide me step by step? Thanks!
Mark44
#15
Jun10-11, 10:18 AM
Mentor
P: 21,397
The compiler has nothing to do with creating a file you use for input. You can use any of the methods given by I like Serena, jhae2,718, or Grep to create your input file.
I like Serena
#16
Jun10-11, 10:34 AM
HW Helper
I like Serena's Avatar
P: 6,189
Quote Quote by stellina View Post
Hey guys, thanks a lot but I am still stuck because I am using xcode as a compiler and there is a different procedure to create a file. I can't figure it out! Is there anyone using xcode that can guide me step by step? Thanks!
Ah, you didn't mention yet that you were working on an Apple with xcode.
I'm not really familiar with it, but I can tell you that with xcode you should be able to create a text file.
Otherwise you can create a text file with TextEdit which should be included somewhere on your Apple.

I can't really tell you where to put the file, or how to run your program in a way that it can find the file.
Perhaps someone familiar with xcode and Apple can!


Register to reply

Related Discussions
Recommended programming language (and texts) for middle-school beginner programming Programming & Computer Science 9
Programming question Engineering, Comp Sci, & Technology Homework 5
Programming Question Computing & Technology 1
Programming Question Computing & Technology 9
C programming question Computing & Technology 8