Solved

Opening a txt file

Posted on 2011-03-11
14
365 Views
Last Modified: 2012-08-13
I am trying to open a file and get the info from it. I have forgotten how to do this. I have the following code but it wont work.
#include<stdio.h>

int main()
{
		FILE *f;
		f = fopen("output.txt", "w");
		
		fputs("Hello Everyone!\n", f);
		
		
		
		
		fclose(f);
		return 0;
}

Open in new window

0
Comment
Question by:HelpMeMaggi
  • 7
  • 5
  • 2
14 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 250 total points
ID: 35114447
#include<stdio.h>

int main()
{
		FILE *f;
		char bufA[1024];
		f = fopen("output.txt", "r");
		
		fgets(bufA, sizeof(bufA), f);
		
		fclose(f);

		fprintf(stderr,"%s\n", bufA);
		return 0;
}

Open in new window

0
 
LVL 10

Expert Comment

by:aboo_s
ID: 35114448
f = fopen("output.txt", "w");
replace with this
f = fopen("output.txt", "r");
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 35114452
f=fopen(“output.txt”,”r”);
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f);
0
 

Author Comment

by:HelpMeMaggi
ID: 35114454
can you please explain. I understand switching the w and r. but not everything else you did.
0
 

Author Comment

by:HelpMeMaggi
ID: 35114465
this did not make a text file.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35114469
I added some comments:

The code you posted works fine for "w"riting the file.  This is how you read the first line from it.

aboo's code reads it one character at a time which is also useful sometimes.

#include<stdio.h>

int main()
{
    // File handle
    FILE *f;

    // Allocate a character array buffer to read the data into
    char bufA[1024];

    // Open the file for "r"ead
    f = fopen("output.txt", "r");

    // Get a string from the file - read it into bufA.
    // Pass the length of bufA so it doesn't overflow.
    fgets(bufA, sizeof(bufA), f);

    // Close the input file
    fclose(f);

    // Print the contents of the buffer to stdout as a string
    fprintf(stdout, "%s", bufA);

    // Return success
    return 0;
}

Open in new window


Here's more info:

http://msdn.microsoft.com/en-us/library/c37dh6kf(v=vs.71).aspx
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35114473
Your comment is confusing about not making a text file.

Are you trying to read a file, or write a file?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:HelpMeMaggi
ID: 35114485
So i have a file with the info in it. Im trying to get that info and print it out. I remember making the txt file with the code but i also know i can do it manually which is what i did now. So i have the file name "output.txt" but i can't read anything off of it. This is all i have as of now....


int main()
{
	FILE *f = fopen("output.txt", "r");
		
		fclose(f);

		
return 0;

}


I erased everything because it was not working.

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35114499
This code should open output.txt (for input), read all the times from it and echo them to the screen.

#include<stdio.h>

int main()
{
    FILE *f;
    char bufA[1024];
    f = fopen("output.txt", "r");
    
    while ( fgets(bufA, sizeof(bufA), f) != NULL )
    {
        fprintf(stderr,"%s", bufA);
    }

    fclose(f);

    return 0;
}

Open in new window

0
 

Author Comment

by:HelpMeMaggi
ID: 35114502
if it helps this is what is in my txt file


5(number of people)
Josh
300.00
1955
Alvin
300.00
1956
Chad
300.00
1966
Ben
300.00
1968
Arnold
300.00
1970

i want to read this file but can't seem to. I've done it before so i know im forgetting something.
0
 

Author Comment

by:HelpMeMaggi
ID: 35114528
If i only wanted to print the names or only the years how would i go about this?
0
 

Author Comment

by:HelpMeMaggi
ID: 35114554
so i can't figure it out.
I am trying to print out "josh owes me 300.00 since 1956".
and so on but separately so they all print out on their own lines.
0
 

Author Comment

by:HelpMeMaggi
ID: 35114830
i can't remove the new lines so its prints out
josh
owes me 300.00
since 1956
how do i get rid of them?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35115995
This will generate the output you showed:

#include<stdio.h>

int main()
{
    FILE *f;
    char bufA[1024];
    f = fopen("output.txt", "r");

    fgets(bufA, sizeof(bufA), f);       // skip the first line
    
    while ( fgets(bufA, sizeof(bufA), f) != NULL )
    {
        // Name starts with a letter
        if ( ( bufA[0] >= 'A' ) && ( bufA[0] <= 'Z' ) ) fprintf(stderr,"%s", bufA);
        else
        {
            char *cP = bufA;
            while ( *cP ) if ( *cP++ == '.' )   // money contains a dot
            {
                fprintf(stderr,"owes me %s", bufA);
                break;
            }
            if ( ! *cP )        // no dot found
            {
                fprintf(stderr,"since %s", bufA);
            }
        }
    
    }

    fclose(f);

    return 0;
}

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now