I'm moving from VB to C,

I've learned about the basics of c upto using arrays.

I'm self-studying & experimenting now how to display text files as databases.

this is the code i use to display text files:

#include<stdio.h>
 
int main()
{
   FILE *f;
   char buf[100];
   f = fopen("test.txt","r");
   fgets(buf,sizeof(buf),f);
   fclose(f);
   printf("%s\n",buf);
   system("Pause");
   return 0;
}

Yeah, i've come across this on the net in a tutorial. :(

This command only prints one line in the text box, but when it comes to multilined text files, it wont display properly. T_T

Do i need the function fread()? If yes how do i call it?

Any suggestions?

You need to put this: fgets(buf,sizeof(buf),f); in a loop that reads until the end of the file.

You probably know how loops work right?

commented: thanks for reminding me +1

yes, i know how they work, thanks for the suggestion, i'm going to try.

But i want a program that would be efficient in terms of memory and time. Isn't loop consuming in both?

EDIT: Here's the results
Using this code

#include<stdio.h>
 
int main()
{
   FILE *f;
   char buf[100];
   f = fopen("test.txt","r");
   int x;
   for(x=1;x<=100;x++)
   {
               fgets(buf,sizeof(buf),f);
   }
   
   
   fclose(f);
   printf("%s\n",buf);
   system("Pause");
   return 0;
}

It only prints the last line of the text file, i'm going to try different types of combination.

EDIT AGAIN: Using this code:

#include<stdio.h>
 
int main()
{
   FILE *f;
   char buf[100];
   f = fopen("test.txt","r");
   int x;
   for(x=1;x<=100;x++)
   {
               fgets(buf,sizeof(buf),f);
               printf("%s\n",buf);
   }
   fclose(f);
   system("Pause");
   return 0;
}

It prints all the contents of the text file, but the last line would continue to loop in printing.

You'll have to put the printf() statement inside the brackets {} of the loop.

Do you know what a flowchart or pseudo-code is? It might help you woth future problems

Is there any command to know how many lines are there in a text file?

I've got some more questions tomorrow, its night here, and i need to sleep.

>Is there any command to know how many lines are there in a text file?
No, you read the file and count them either by using line-oriented input or by manually counting newline characters.

I think he's more interested in how many lines are in the file for his loop. to answer that, fgets will return NULL on end-of-file. So you might want to do something like:

while (fgets(......) != NULL)
{
      printf(.....);
}

I think he's more interested in how many lines are in the file for his loop. to answer that, fgets will return NULL on end-of-file. So you might want to do something like:

while (fgets(......) != NULL)
{
      printf(.....);
}

Thanks niek_e, i didn't know you can use the fgets command as a condition. I'll be working on my VB project for a while to correct some bugs and i'll look into this later.

Is it possible to make a text file with a filename according to your input?

I think it goes like this but it doesn't work.

#include<stdio.h>
int main()
{
   char *makefile[20];
   scanf("%s", &makefile);
   FILE *f;
   f = fopen("%s.txt","w", makefile);
   return 0;
}

T_T any other suggestions?

fopen doesn't support formatting specifiers. You need to build the string manually before passing it to fopen:

#include <stdio.h>

int main ( void )
{
  char filename[24];

  if ( scanf ( "%19s", filename ) == 1 ) {
    FILE *out;

    strcat ( filename, ".txt" );
    out = fopen ( filename, "w" );

    if ( out != NULL ) {
      /* Write to the file */

      fclose ( out );
    }
  }

  return 0;
}
commented: very helpful +1

Thanks Narue!

That online tutorial page just stopped there.
It is a good thing people here are helpful.

I just finally found out how to make a gui using windows.h, im still studying how to use it, and i plan to to do some stuff with what i've learned so far.

I cant seem to find a code telling me how to use and call a drop-down list box.

Is it possible using a textbox as an input, and then store it in a text file? The input i'm refereing to is multilined in nature. This hapens when I press a text button.

HWND hwndText = CreateWindow(
                               TEXT("edit"),                              // The class name required is combobox
                               TEXT("TEXTBOX test"),                                 // Default text.
                               WS_VISIBLE | WS_CHILD | WS_BORDER | WS_HSCROLL | WS_VSCROLL| ES_MULTILINE | ES_AUTOHSCROLL, // the styles
                               200,200,                                      // the left and top co-ordinates
                               100,100,                                  // width and height
                               hwnd,                                     // parent window handle
                               (HMENU)13,                         // the ID of your combobox
                               hThisInstance,                                // the instance of your application
                               NULL
                            );

I thought creating a gui is hard, but its was actually not that hard.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.