• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

Opening a txt file

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
HelpMeMaggi
Asked:
HelpMeMaggi
  • 7
  • 5
  • 2
1 Solution
 
sjklein42Commented:
#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
 
aboo_sCommented:
f = fopen("output.txt", "w");
replace with this
f = fopen("output.txt", "r");
0
 
aboo_sCommented:
f=fopen(“output.txt”,”r”);
while((c=getc(f1))!=EOF)
printf(“%c”,c);
fclose(f);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HelpMeMaggiAuthor Commented:
can you please explain. I understand switching the w and r. but not everything else you did.
0
 
HelpMeMaggiAuthor Commented:
this did not make a text file.
0
 
sjklein42Commented:
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
 
sjklein42Commented:
Your comment is confusing about not making a text file.

Are you trying to read a file, or write a file?
0
 
HelpMeMaggiAuthor Commented:
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
 
sjklein42Commented:
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
 
HelpMeMaggiAuthor Commented:
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
 
HelpMeMaggiAuthor Commented:
If i only wanted to print the names or only the years how would i go about this?
0
 
HelpMeMaggiAuthor Commented:
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
 
HelpMeMaggiAuthor Commented:
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
 
sjklein42Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now