# C programming question

by stellina
Tags: programming
 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 #include 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
 P: 324 The code tags work fine for me: #include #include 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.
 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.
 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");
 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?
Mentor
P: 21,397
 Quote by stellina 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 by stellina 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.
 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).
 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.
 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!
HW Helper
P: 6,189
Hi stellina!

 Quote by stellina 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. ;)
 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.
 PF Gold 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.
 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.
 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!
 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.
HW Helper
P: 6,189
 Quote by stellina 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!

 Related Discussions Programming & Computer Science 9 Engineering, Comp Sci, & Technology Homework 5 Computing & Technology 1 Computing & Technology 9 Computing & Technology 8